2011-04-19 40 views
3

我已經得出結論,我應該在My Code-First設計中定義獨立協會和外鍵協會。 e.g:更新實體框架中的外鍵關聯4.1代碼優先

public class Book 
{ 
    public int ID {get; set;} 
    public int AuthorID {get; set;} 
    [ForeignKey("AuthorID")] 
    public Author Author {get; set;} 
} 
  1. 上述定義,我必須更新的AuthorID的時候我想改變這本書的作者,或者只是使用下面的線是夠嗎?
    myBook.Author = author;

  2. 如果這是我第一次爲本書定義作者,我是否會在上面的行中獲得null異常? (不EF自動初始化書的作者時,我給你一定的參考價值嗎?)我應該初始化它的定義:

代碼:

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

回答

4

首先,你不能同時使用independent and foreign key association - 您使用第一或第二。不同之處在於你是否使用FK屬性。如果您使用外鍵關聯,則應該使用外鍵來建立關係。這就是FK協會在EFv4中引入的原因。

編輯:

簡單的例子,爲什麼你應該使用自定義波蘇斯(在EFv4.1常見)和FK關係時使用的,而不是導航屬性FK:

這工作沒有任何問題:

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

這再次WOR KS沒有任何問題:

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

請大家在看第二個例子中的 「創建和修改關係」:HTTP:// MSDN .microsoft.com/en-us/library/ee373856.aspx爲什麼分配導航屬性不會引發異常? (在第二個例子中,只是navigatrion屬性被更新。) – Kamyar 2011-04-20 06:16:54

2

以下示例有同樣的問題:

public class PingPongPlayer 
{ 
    [Key] 
    public string Name { get; set; } 
    public string EMail { get; set; } 
    public int Ranking { get; set; } 
} 

public class Match 
{ 
    [Key] 
    public int Id { get; set; } 

    public string FrkPlayer1 { get; set; } 
    public string FrkPlayer2 { get; set; } 

    [ForeignKey("FrkPlayer1")] 
    public PingPongPlayer Player1 { get; set; } 

    [ForeignKey("FrkPlayer2")] 
    public PingPongPlayer Player2 { get; set; } 

    public DateTime MatchDate { get; set; } 

    public bool AlreadyPlayed { get; set; } 

    public string Player1Name 
    { 
     get { return Player1.Name; } 
    } 

    public string Player2Name 
    { 
     get { return Player2.Name; } 
    } 
} 

如果我控件綁定到Player1Name財產,我得到一個NullPointerException的屬性。在數據庫中,我可以看到表格,它似乎有正確的鍵值。

Name EMail Ranking <br> 
a [email protected] 10 <br> 
b [email protected] 15 <br> 
c [email protected] 12 <br> 
d [email protected] 20 <br> 

Id FrkPlayer1 FrkPlayer2 MatchDate AlreadyPlayed 
1 a   b   2011-04-21 00:00:00.000 0 
2 a   c   2011-04-21 00:00:00.000 0 
3 b   c   2011-04-21 00:00:00.000 0 
4 a   d   2011-04-21 00:00:00.000 0 
5 a   c   2011-04-21 00:00:00.000 0 
6 d   c   2011-04-21 00:00:00.000 0 

要解決該問題,只需更換:

[ForeignKey("FrkPlayer1")] 
public PingPongPlayer Player1 { get; set; } 

[ForeignKey("FrkPlayer2")] 
public PingPongPlayer Player2 { get; set; } 

通過

[ForeignKey("FrkPlayer1")] 
public virtual PingPongPlayer Player1 { get; set; } 

[ForeignKey("FrkPlayer2")] 
public virtual PingPongPlayer Player2 { get; set; } 
+0

嗯!感謝關於如何使用延遲加載的提示。 – Kamyar 2011-04-23 04:01:41

相關問題