Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 如何在EF5中添加具有子实体但不更新这些子实体的实体_Entity Framework - Fatal编程技术网

Entity framework 如何在EF5中添加具有子实体但不更新这些子实体的实体

Entity framework 如何在EF5中添加具有子实体但不更新这些子实体的实体,entity-framework,Entity Framework,我正在Visual Studio 2012中使用EF5和POCO类以及.Net Framework 4.5。我有这样的亲子关系: public class Parent { [Key] public int Id {set; get} public string Name {set; get;} public int ChildId {set; get;} public virtual Child Child { get; set; } } public class Chi

我正在Visual Studio 2012中使用EF5和POCO类以及.Net Framework 4.5。我有这样的亲子关系:

public class Parent {
  [Key]
  public int Id {set; get}
  public string Name {set; get;}
  public int ChildId {set; get;}
  public virtual Child Child { get; set; }
}

public class Child {
      [Key]
      public int Id {set; get}
      public string Name {set; get;}
}
{ 
Id: 0, 
Name:'Spock', 
ChildId: 42
}
    parent.Child= (from child in db.Child where child.Id == parent.ChildId select child).FirstOrDefault();
    db.Parent.Attach(parent);
db.Entry(parent).State = System.Data.EntityState.Added;
db.Parent.Add(parent);
我正在Web Api场景中使用EF5。客户端希望通过如下方式发送JSON来创建新的父级:

public class Parent {
  [Key]
  public int Id {set; get}
  public string Name {set; get;}
  public int ChildId {set; get;}
  public virtual Child Child { get; set; }
}

public class Child {
      [Key]
      public int Id {set; get}
      public string Name {set; get;}
}
{ 
Id: 0, 
Name:'Spock', 
ChildId: 42
}
    parent.Child= (from child in db.Child where child.Id == parent.ChildId select child).FirstOrDefault();
    db.Parent.Attach(parent);
db.Entry(parent).State = System.Data.EntityState.Added;
db.Parent.Add(parent);
javascript客户端不必提供子对象,只需提供键

但是,要做到这一点,这意味着在Web Api方法中,我必须读取子实体并将其设置为Parent.Child,如下所示:

public class Parent {
  [Key]
  public int Id {set; get}
  public string Name {set; get;}
  public int ChildId {set; get;}
  public virtual Child Child { get; set; }
}

public class Child {
      [Key]
      public int Id {set; get}
      public string Name {set; get;}
}
{ 
Id: 0, 
Name:'Spock', 
ChildId: 42
}
    parent.Child= (from child in db.Child where child.Id == parent.ChildId select child).FirstOrDefault();
    db.Parent.Attach(parent);
db.Entry(parent).State = System.Data.EntityState.Added;
db.Parent.Add(parent);
如果我没有将parent.Child设置为与键对应的实例,则EF会引发异常:

ExceptionMessage=A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship.
我还尝试添加新的父对象,如下所示:

public class Parent {
  [Key]
  public int Id {set; get}
  public string Name {set; get;}
  public int ChildId {set; get;}
  public virtual Child Child { get; set; }
}

public class Child {
      [Key]
      public int Id {set; get}
      public string Name {set; get;}
}
{ 
Id: 0, 
Name:'Spock', 
ChildId: 42
}
    parent.Child= (from child in db.Child where child.Id == parent.ChildId select child).FirstOrDefault();
    db.Parent.Attach(parent);
db.Entry(parent).State = System.Data.EntityState.Added;
db.Parent.Add(parent);
其中parent.Child为null,但实体框架生气地说:

ExceptionMessage=Cannot insert the value NULL into column 'Name', table 'MyDB.dbo.Child'; column does not allow nulls. INSERT fails.
The statement has been terminated.
有没有一种方法可以插入一个新的父行,而不必读取子行的所有实例,只设置它们的键?我希望父POCO类在获取(读取)数据时填充子类,因此我希望那里有虚拟子属性,但我不希望客户端能够更新子类。我是否可以告诉EF在添加或附加过程中忽略子项。在我的例子中,一对一的关系中有一个独生子女,但是在一对多的关系中可能有很多,所以这可能会变得非常难看,并且会造成巨大的性能损失

总之,我根本不希望EF创建子行或更新子行,我如何阻止它以不涉及从数据库读取和设置所有子实体的方式更新子实体?

此关系中的“主体”实体是子实体(在添加父记录之前,子记录必须存在于数据库中)“。依赖”实体是父实体,只有在子实体在数据库中可用时才能添加。您的名称与此不匹配

子实体可能应该具有父导航属性

public ICollection<Parent> Parents { get; set; }
然后,只要子记录已经存在于数据库中,您就可以简单地编写

db.Parents.Add(newParent);

A.在这个例子中,用“产品”代替“父产品”,用“类别”代替“子产品”是完全正确的如果我将navigator添加回父级,那么我将需要处理Json自引用循环问题,然后关闭ProxyCreation,然后找出急切的加载或查询路径。C.我还没有找到一个好例子,有很多文章试图解决这个问题。是吗有一个大家都知道的综合示例?D。EF6在处理这种情况方面是否更好?或者您使用的是ViewModel。通常您创建的ViewModel只在视图和控制器之间来回传递所需的属性。这些属性可能与您的实体相同,没有导航属性,但通常它们会有所不同租金,带有投影数据和/或计算字段。在复杂实体中,它们通常只是您正在使用的视图所需的属性。