Since the Entity Framework creates proxy instead of providing the "original" entity classes, how do you cast a parent class to a child class?
This does not work "the normal way" because the automatically created proxy classes don't use the inheritance structure of the original entity classes.
Turning off the proxy-creation feature is not an option for me.
Any help is welcome, thanks!
How do you cast a parent class to a child class?
You can only cast a parent to a child if the actual runtime type is a child. That's true for non-proxies and proxies because a proxy of a child derives from a child, hence it is a child. A proxy of a parent is not a child, so you can't cast it to a child, neither can you cast a parent to a child.
For example (using DbContext API):
public class Parent { ... }
public class Child : Parent { ... }
Then both the following casts will work:
Parent parent1 = context.Parents.Create<Child>(); // proxy
Parent parent2 = new Child(); // non-proxy
Child child1 = (Child)parent1; // works
Child child2 = (Child)parent2; // works
And both the following won't work:
Parent parent1 = context.Parents.Create<Parent>(); // proxy
Parent parent2 = new Parent(); // non-proxy
Child child1 = (Child)parent1; // InvalidCastException
Child child2 = (Child)parent2; // InvalidCastException
Both casts work "the normal way".
Related
Hi I am using Spring Boot app with MVC. I have two model classes Parent and Child. Parent to Child has OnetoMany Mapping. I have a form submit in which I'm passing few fields of child class, now what is happening when I bind the child class in the controller method is that it picks up the id of Parent class and binds it automatically to Child object in Controller.Please suggest what I am missing in here.
Following is the code for the same:
#PostMapping("/{id}/child")
public String editChildPOST(#PathVariable Long id,
Child child, Model model, BindingResult errors,
Principal principal) {
logger.info("editChildPOST: " + child.getId);
model.addAttribute("aaaa", aaaa);
model.addAttribute("bbbb", "bbbb");
return "redirect:/xxxx";
}
Following is the mapping in the parent Class :
class Parent{
#OneToMany(cascade= ALL, orphanRemoval=true)
#JoinColumn(name="parent_id")
private List<Child> child;
}
SO in debug mode if I try to check the value of child in Controller it shows Child Id as parent's Id although I haven't set Child Id anywhere in the form submit.
I'm using EF6 (6.1.3 I think) in a web api application. For some odd reason my child collection navigation will not insert when I insert a parent object something in a disconnected context scenario like below:
var child1 = new Child { /*fill values */ };
var child2 = new Child { /*fill values */ };
var children = new List<Child>();
children.Add(child1);
children.Add(child2);
var parent = new Parent
{
// fill other properties
Children = children
}
dbContext.Parents.Add(parent);
dbContext.SaveChanges();
Can you please tell me what am I doing wrong? I thought adding an object to a context would automatically add all children in the entity graph. Also trying to get this to work as part of a Repository pattern. Super confused about the whole thing.
So here's my screnario. I have a toolbar at the top (office style), with buttons. This is hosted in a shell. Some of those buttons are applicable only to certain child view models as they get loaded. Ideally what I would like to happen is have the buttons action.target repositioned to child view model as it gets created (I kind of got this working by settings Action.Target="ActiveItem" on them. This doesn't solve the problem fully though:
a) When the child viewmodel is closed and there is no active item, I want them to reposition to Shell as the target so they can be set to "default" state.
b) I noticed that when child viewmodel is closed and the shell being the conductor has it ActiveItem=null, the hooks from the action are still bound to the living instance of the last viewmodel, so doesn't looks like it got disposed of. Memory leak?
Any suggestions how to implement this scenario?
What about adding a property to your ShellViewModel which points to the action target and updating it when stuff gets activated/deactivated:
e.g.
public class ShellViewModel
{
public object ActionTarget
{
get { return _actionTarget; }
set
{
_actionTarget = value;
NotifyOfPropertyChange(() => ActionTarget);
}
}
// Then when the active item changes just update the target:
public override NotifyOfPropertyChange(string propertyName)
{
if(propertyName == "ActiveItem")
{
if(ActiveItem == null) ActionTarget = this;
else ActionTarget = ActiveItem;
}
}
}
Now bind to that:
<SomeMenu cal:Action.Target="{Binding ActionTarget}" />
Not sure if that will work or not but I'm sure I've done something similar in the past. (You may also have to explicitly call NPC on your actions before they will update after you have changed ActiveItem)
Im using treeview in asp.net
how can i check if parent contains childnodes in treeview selected node changed event.
In case you want to look if the parent of the selected node contains other children nodes, it is safe to say
bool ContainsOtherChildren = treeView1.SelectedNode.Parnet.ChildNodes.Count > 1;
since you know that it already has at least one child node (the selected one)
I would however make another check if there is indeed a parent such as
if(treeView1.SelectedNode.Parent != null)
{
ContainsOtherChildren = treeView1.SelectedNode.Parnet.ChildNodes.Count > 1;
}
Check All the child pointer values, whether is it NULL or not.
If all child pointer value is NULL , you can ensure that the parent does not have any child.
I am working in air application , i need to know how to add event listener when any object is updated, how can i implement this.
Example: i have Class name Vehicle, and child class are Car,Bus,Bikes,Scooter,..etc, child class also have many properties like color,model no,....etc
I have array collection and AddChild() method in Vehicle class by this, i will add, child class to the vehicle class.
I need a event listener which can trigger if any of the property is updated or changed in any of the child class property, how can i implements this in Flex3.
I need this for knowing, is there any update happen in the Object.
Thanks In Advance
One thing you can do is to make getters and setters for the properties instead of public vars and have your class extend EventDispatcher, if it does not already do so because it is extended from a MovieClip, like:
private var _vehicleName:String;
.
.
.
public function set vehicleName(value:String):void {
_vehicleName = value;
dispatchEvent(new VehicleEvent(VehicleEvent.propertyChange, "vehicleName"));
}
public function get vehicleName():String {
return _vehicleName;
}
(VehicleEvent being an extended class of Event with an extra string to signify which property changed)
Then you can add an eventlistener to the vehicles and they will dispatch events when the properties defined in this way change.
If you use an Array Collection for the vehicles and if you make the properties of the vehicles Bindable as Amarghosh suggests, then the array collection already should throw an Collection Event of the kind UPDATE. It also tells you which items (Vehicles) were updated an afaik also which properties were updated. But in general it is easier to directly bind to the property, like Amarghosh says.