在 Entity Framework 4.1 Code-First 中更新外键关联 [英] Updating Foreign key associations in Entity Framework 4.1 Code-First

查看:32
本文介绍了在 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;} 
}  

  1. 有了上面的定义,当我想改变书的作者时,我是否必须更新AuthorID,还是只使用下面一行就足够了?
    myBook.Author = 作者;

  1. 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 中更新外键关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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