更新实体框架4.1 code-第一外键关联 [英] Updating Foreign key associations in Entity Framework 4.1 Code-First

查看:148
本文介绍了更新实体框架4.1 code-第一外键关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得出一个结论,我应该在code-首先设计定义两个独立协会和外键关联。例如:

 公共类图书
{
  公众诠释ID {获得;组;}
  公众诠释的AuthorID {获得;组;}
  [ForeignKey的(的AuthorID)
  公共作者作者{获得;组;}
}
 

  1. 通过上面的定义,我必须更新的AuthorID的时候我想改变这本书的作者,或者只是使用下面的线就足够了?
    myBook.Author =作者;

  2. 难道我会得到一个空的异常上述行,如果这是我第一次界定一个作家的书? (EF是否自动初始化本书的作者时,我给你一定的参考价值吗?)我应该在定义中初始化:

在code:

 公共类图书
{
  公众诠释ID {获得;组;}
  公众诠释的AuthorID {获得;组;}

  私人作者m_Author;
  [ForeignKey的(的AuthorID)
  公共作者作者{获得
  {
    得到
    {
      如果(m_Author == NULL)
        m_Author =新的作者();
      返回m_Author;
    }
    组
    {
      this.m_Author =价值;
    }
  }
}
 

解决方案

首先,你不能同时使用<一个href="http://stackoverflow.com/questions/5281974/$c$c-first-independent-associations-vs-foreign-key-associations/5282275#5282275">independent和外键关联 - 您使用的第一或第二。所不同的是,如果你使用的FK财产与否。如果你使用外键关联,你应该使用外键来建立关系。这就是为什么FK协会是在EFv4推出的原因。

编辑:

简单的例子,为什么你使用自定义波苏斯(在EFv4.1常见)和FK的关系时,应使用,而不是导航属性FK:

这工作没有任何问题:

  VAR的孩子=新ChildEntity(){n = 1};
child.ParentEntityId = 1; //分配FK
context.Childs.Attach(子);
context.Entry(子).​​State = EntityState.Modified;
context.SaveChanges();
 

这抛出异常:

  VAR父=新ParentEntity(){n = 1};
context.Parents.Attach(父);
VAR的孩子=新ChildEntity(){n = 1};
child.Parent =父母; //&LT;  - 分配唯一导航属性
//下一行将会导致出现InvalidOperationException:
//参照完整性约束冲突发生:
//定义引用约束的属性值
//不本金和依赖对象之间是一致的
//关系。
context.Childs.Attach(子);
context.Entry(子).​​State = EntityState.Modified;
context.SaveChanges();
 

这一次的作品没有任何问题的:

  VAR父=新ParentEntity(){n = 1};
context.Parents.Attach(父);
VAR的孩子=新ChildEntity(){n = 1};
child.Parent =父母;
child.ParentEntityId = 1; //&LT;  - 再次分配FK
context.Childs.Attach(子);
context.Entry(子).​​State = EntityState.Modified;
context.SaveChanges();
 

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. 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;

  2. 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:

The code:

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;
    }
  } 
}

解决方案

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.

Edit:

Simple example why you should use FK instead of navigation property when using custom POCOs (common in EFv4.1) and FK relations:

This works without any problem:

var child = new ChildEntity() {Id = 1};
child.ParentEntityId = 1;  // Assigning FK
context.Childs.Attach(child);
context.Entry(child).State = EntityState.Modified;
context.SaveChanges();

This throws exception:

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();

This again works without any problem:

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();

这篇关于更新实体框架4.1 code-第一外键关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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