2011-12-19 85 views
0

今天我一直在第一次使用MVC。通常情況下,我通常首先使用EF模型,但我想嘗試POCO。MVC - 實體框架 - 元數據關係

所以我做了我的3個實體,當我嘗試做一個控制器,我得到一個錯誤:

Unable to retrieve metadata for "BookExchange.Models.Exchange". Unable to determine the principal end of an association between the types "BookExchange.Models.Exchange" and "BookExchange.Models.Book". The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

我的3類:

public class Book 
{ 
    public int BookID { get; set; } 
    public string Name { get; set; } 
    public string Author { get; set; } 
    public string ISBN10 { get; set; } 

    public int PersonID { get; set; } 
    public virtual Person Person { get; set; } 

    public virtual Exchange Exchange { get; set; } 
} 

public class Person 
{ 
    public int PersonID { get; set; } 
    public string Name { get; set; } 
    public virtual ICollection<Book> Books { get; set; } 
    public virtual ICollection<Exchange> Exchanges { get; set; } 
} 

public class Exchange 
{ 
    [Key] 
    public int BookID { get; set; } 

    public int PersonID { get; set; } 
    public DateTime ReturnDate { get; set; } 

    public virtual Person Person { get; set; } 
    public virtual Book Book { get; set; } 
} 

有誰知道我是什麼做錯了?我不想失去關聯屬性。

在此先感謝!

+0

您可能需要添加FK屬性。 – jrummell 2011-12-19 14:24:04

+0

我不知道在哪裏。有任何提示呢?以及如何申報。 – Julian 2011-12-19 14:28:09

+0

在Exchange中刪除此項:「公共虛擬Exchange Exchange {get; set;}」或「公共虛擬書本{get; set;}」。在下面檢查我的答案。 – NicoJuicy 2011-12-21 14:42:47

回答

0

嘗試爲您的引用添加外鍵屬性。例如。

public class Book 
{ 
    public int BookID { get; set; } 
    public string Name { get; set; } 
    public string Author { get; set; } 
    public string ISBN10 { get; set; } 

    public virtual Person Person { get; set; } 
    public int PersonID { get; set; } 
    public virtual Exchange Exchange { get; set; } 
    public int ExchangeID { get; set; } 
} 

public class Person 
{ 
    public int PersonID { get; set; } 
    public string Name { get; set; } 
    public virtual ICollection<Book> Books { get; set; } 
    public virtual ICollection<Exchange> Exchanges { get; set; } 
} 

public class Exchange 
{ 
    public int ExchangeID { get; set; } 

    public int BookID { get; set; } 
    public virtual Book Book { get; set; } 
    public int PersonID { get; set; } 
    public virtual Person Person { get; set; } 

    public DateTime ReturnDate { get; set; } 
} 

而且,看看ScottGu's post on code firstthis EF post on conventions

+0

我剛剛複製並將所有代碼粘貼到類中。嘗試添加控制器時出現以下錯誤:「無法檢索'BookExchange.Models.Person'的元數據無法確定類型」BookExchange.Models.Exchange「和」BookExchange.Models「之間關聯的主體端.Book'。此關聯的主要目的必須使用關係流暢API或數據註釋來顯式配置。 – Julian 2011-12-19 14:56:14

+0

EF試圖根據命名約定來推斷您的關係。在這種情況下,BookID是Book in Exchange的FK。但是,Person.OwnedBooks和Person.RentedBooks不能被推斷,因爲它們不遵循約定,您可以將它們更改爲Person.Books和Person.Exchanges,或者使用Fluent API或數據註釋屬性將它們明確映射到 – jrummell 2011-12-19 15:01:52

+0

這不是問題,我將它們改爲Books and Exchange,但這沒有幫助。 – Julian 2011-12-19 15:06:55

0

試試這個:(刪除數據庫,因此EF將創造新)

public class Book 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public string Author { get; set; } 
    public string ISBN { get; set; } 

    public virtual Person Person { get; set; } 
    public virtual Exchange Exchange { get; set; } 
} 

public class Person 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 

    public virtual ICollection<Book> Books { get; set; } 
    public virtual ICollection<Exchange> Exchanges { get; set; } 
} 

public class Exchange 
{  
    public int Id { get; set; } 
    public DateTime ReturnDate { get; set; } 

    public virtual Person Person { get; set; } 
    public virtual Book Book { get; set; } 
} 
0

這是你的一對一關聯。

交換或書本的刪除一個參考,所以代碼可以先決定哪一個是你的一對一關係(圖書< - >交易所)更重要。如果你想知道爲什麼

you should read this: