Click here to Skip to main content
15,921,210 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone

I'm going postal for this :)

given two entities:

C#
public class parent{
    int parentid {get;set;}
    public virtual ICollection<child> childre{get; set;}
}

public class child{
    int childid{get;Set;}
    public virtual parent parent{get;set;}
}</child>


can I do something like

C#
public actionresult create(parent parent){
 
  child newchild = new (child);
  parent.children.add(child); 
  context.parent.add(parent);
}


should'nt this create the parent entity with added child(children)? How can I do this?

do I have to do it separately???

C#
public actionresult create(parent parent){
 
  child newchild = new (child);

  context.parent.add(parent);

  child.parent=parent;
  context.child.add(child);
}


Thank you in advance,

Alberto.
Posted

The code below should fix what you are trying to do. There were only a couple of typo's in the code every thing else looked good.

C#
public class parent{
    int parentId {get;set;}
    public virtual ICollection<child> children{get; set;}
}
 
public class child{
    int childId{get;Set;}
    public virtual parent childParent{get;set;}
}

public actionresult create(parent parent){
 
  child newchild = new child();
  parent.children.add(newchild); 
  context.parent.add(parent);
  return View(parent);
}

</child>
 
Share this answer
 
Comments
Alberto Biasiutti 4-Dec-11 15:54pm    
Thank you GK... the typo's were not the problem (I wrote the question VEEERY quickly because I was in hurry).. thank you any way, I'll post the solution my boss foundout.
First of all, I apologize: I asked the question whitout being precise. The problem was that, when calling the create function, it gave a null reference exeption on parent.children.add(child):

C#
public actionresult create(parent parent){
 
  child newchild = new (child);
  parent.children.add(child);  /*<-- Null Reference exeption*/
  context.parent.add(parent);
}


The actual problem was that, before the parent is created, the ICollection<child> doesn't exists. It is sufficient to instantiate it before adding elements to it.

this way it works, automatically adding both the parent and the child entities (in the previous example, I also forgot about the context.SaveChanges() instruction to actually save the objects to database):

C#
public actionresult create(parent parent){
 
  child newchild = new (child);
  parent.children=new List<child>();
  parent.children.add(child);
  context.parent.add(parent);
  context.SaveChanges();
}</child>


I have a little doubt about the correctness of setting an ICollection to a new List, so if anyone has a better or more correct approach, any suggestion is welcome.
 
Share this answer
 
v2
Comments
GateKeeper22 5-Dec-11 12:45pm    
I would change the children collection property to us a private variable and new the private variable in the declaration. That way you won't have to do it in every place that you use a new parent.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900