与孩子一起创建新实体 [英] Create new Entity with children

查看:63
本文介绍了与孩子一起创建新实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

为此我要去邮寄:)

给定两个实体:

 公共  class 父级{
     int  parentid { get ;  set ;}
    公共 虚拟 ICollection< child> childre {获取; ;}
}

公共 孩子{
     int  childid { get ; Set;}
    公共 虚拟父级父级{获取 set ;}
} </ 子级 >  



我可以做类似的事情吗?

  public  actionresult create(parent parent){
 
  child newchild = (子级);
  parent.children.添加(子级);
  context.parent.添加(父级);
} 



这不应该创建带有添加的子代的父实体吗?我该怎么办?

我必须单独做吗?

  public  actionresult create(parent parent){
 
  child newchild = (子级);

  context.parent.添加(父);

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



预先谢谢您,

Alberto.

解决方案

下面的代码应该可以解决您尝试做的事情.代码中只有几个错字,其他所有东西看起来都很不错.

 公共  class 父级{
     int  parentId { get ;  set ;}
    公共 虚拟 ICollection< child>孩子们{获取;}
}
 
公共 孩子{
     int  childId { get ; Set;}
    公共 虚拟父childParent {获取 set ;}
}

公共操作结果创建(父级父级){
 
  child newchild =  child();
  parent.children. add (newchild);
  context.parent.添加(父级);
  返回视图(父级);
}

</  child  >  上给出了一个空引用实例.

  public  actionresult create(parent parent){
 
  child newchild = (子级);
  parent.children.添加(子级); /*  <-空引用示例*/
  context.parent.添加(父级);
} 



实际的问题是,在创建父级之前,ICollection< child>不存在.在向其添加元素之前对其进行实例化就足够了.

通过这种方式,它会自动添加父实体和子实体(在上一示例中,我也忘记了context.SaveChanges()指令实际上将对象保存到数据库中):

  public  actionresult create(parent parent){
 
  child newchild = (子级);
  parent.children = 列表< child>();
  parent.children.添加(子级);
  context.parent.添加(父级);
  context.SaveChanges();
} </ 子级 >  



我对将ICollection设置为新List的正确性存有疑问,因此,如果有人有更好或更正确的方法,欢迎提出任何建议.


Hi everyone

I''m going postal for this :)

given two entities:

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

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???

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.

解决方案

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.

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>


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):

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):

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.


这篇关于与孩子一起创建新实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆