【发布时间】:2014-01-02 17:05:18
【问题描述】:
我在 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;}
}
我在 Web Api 场景中使用 EF5。客户端希望通过发送 JSON 来创建一个新的 Parent:
{
Id: 0,
Name:'Spock',
ChildId: 42
}
javascript 客户端不必提供子对象,只需提供键即可。
但是,要做到这一点,这意味着在 Web Api 方法中,我必须读取 Child 实体并将其设置为 Parent.Child,如下所示:
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;
如果我没有将 parent.Child 设置为 key 对应的实例,那么 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.
我也尝试过像这样添加新的父对象:
db.Parent.Add(parent);
其中 parent.Child 为 null,但随后 Entity Framework 生气并说:
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.
有没有一种方法可以插入一个新的 Parent 行,而无需读取所有子实例,只需设置它们的键?我希望 Parent POCO 类在 GET(读取)数据时填充子级,所以我想要那里的虚拟子属性,但我不希望客户端能够更新子级。我可以以某种方式告诉 EF 在添加或附加期间忽略孩子吗?在我的示例中,有一个孩子处于 1:1 的关系中,但可以有很多和 1:many 的关系,所以这可能会变得非常丑陋,并且会对性能造成很大的影响。
总之,我根本不希望 EF 创建子行或更新子行,如何阻止它以不涉及从数据库中读取和设置所有子实体的方式更新子实体?
【问题讨论】:
标签: entity-framework