Updating Foreign key associations in Entity Framework 4.1 Code-First(在 Entity Framework 4.1 Code-First 中更新外键关联)
问题描述
我得出的结论是,我应该在我的 Code-First 设计中同时定义独立关联和外键关联.例如:
I have come to a conclusion that I should define both Independent Association and Foreign Key Association in My Code-First design. e.g:
public class Book
{
public int ID {get; set;}
public int AuthorID {get; set;}
[ForeignKey("AuthorID")]
public Author Author {get; set;}
}
有了上面的定义,当我想改变书的作者时,我是否必须更新AuthorID,还是只使用下面一行就足够了?
myBook.Author = 作者;
With the above definition, do I have to update AuthorID when I want to change the book's author, Or just using the below line is enough?
myBook.Author = author;
如果这是我第一次为这本书定义作者,我会在上面的行中得到一个空异常吗?(当我为它分配一些值时,EF 会自动初始化书的作者吗?)我应该在定义中初始化它:
Am I going to get a null exception on the above line if that is the first time I'm defining an author for the book? (Does EF initialize book's author automatically when I assign some value to it?) Should I initialize it in the definition:
代码:
public class Book
{
public int ID {get; set;}
public int AuthorID {get; set;}
private Author m_Author;
[ForeignKey("AuthorID")]
public Author Author {get
{
get
{
if (m_Author == null)
m_Author = new Author();
return m_Author;
}
set
{
this.m_Author = value;
}
}
}
推荐答案
首先不能同时使用独立和外键关联 - 您使用第一个或第二个.不同之处在于您是否使用 FK 属性.如果您使用外键关联,您应该使用外键来建立关系.这就是在 EFv4 中引入 FK 关联的原因.
First of all you can't use both independent and foreign key association - you use either first or second. The difference is if you use FK property or not. If you use foreign key association you should use foreign key to build a relation. That is the reason why FK association was introduced in EFv4.
在使用自定义 POCO(在 EFv4.1 中很常见)和 FK 关系时为什么应该使用 FK 而不是导航属性的简单示例:
Simple example why you should use FK instead of navigation property when using custom POCOs (common in EFv4.1) and FK relations:
这没有任何问题:
var child = new ChildEntity() {Id = 1};
child.ParentEntityId = 1; // Assigning FK
context.Childs.Attach(child);
context.Entry(child).State = EntityState.Modified;
context.SaveChanges();
这会引发异常:
var parent = new ParentEntity() { Id = 1 };
context.Parents.Attach(parent);
var child = new ChildEntity() {Id = 1};
child.Parent = parent; // <-- Assigning only navigation property
// Next line will cause InvalidOperationException:
// 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.
context.Childs.Attach(child);
context.Entry(child).State = EntityState.Modified;
context.SaveChanges();
这又可以正常工作了:
var parent = new ParentEntity() { Id = 1 };
context.Parents.Attach(parent);
var child = new ChildEntity() {Id = 1};
child.Parent = parent;
child.ParentEntityId = 1; // <-- AGAIN assigning FK
context.Childs.Attach(child);
context.Entry(child).State = EntityState.Modified;
context.SaveChanges();
这篇关于在 Entity Framework 4.1 Code-First 中更新外键关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Entity Framework 4.1 Code-First 中更新外键关联
- 输入按键事件处理程序 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01