今天我一直在第一次使用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; }
}
有誰知道我是什麼做錯了?我不想失去關聯屬性。
在此先感謝!
您可能需要添加FK屬性。 – jrummell 2011-12-19 14:24:04
我不知道在哪裏。有任何提示呢?以及如何申報。 – Julian 2011-12-19 14:28:09
在Exchange中刪除此項:「公共虛擬Exchange Exchange {get; set;}」或「公共虛擬書本{get; set;}」。在下面檢查我的答案。 – NicoJuicy 2011-12-21 14:42:47