MVC - 实体框架 - 元数据关系
今天我第一次使用 MVC。通常我首先使用带有模型的 EF,但我想尝试 POCO。
所以我已经创建了 3 个实体,当我尝试创建控制器时出现错误:
无法检索“BookExchange.Models.Exchange”的元数据。无法确定类型“BookExchange.Models.Exchange”和“BookExchange.Models.Book”之间关联的主体端。此关联的主体必须使用关系流畅 API 或数据注释显式配置。
我的三堂课:
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; }
}
有谁知道我做错了什么?我不想失去关联属性。
提前致谢!
Today I've been working with MVC for the first time. Also normally I use the EF with model first, but I wanted to try POCO.
So I've made my 3 entities and when I try to make a controller I get an error:
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.
My 3 classes:
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; }
}
Does anyone know what I'm doing wrong? I don't want to lose the association properties.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试添加外键属性以供参考。例如
,另外,看看 ScottGu 关于代码优先的帖子 和 这篇关于约定的 EF 帖子 。
Try adding foreign key properties for your references. E.g.
Also, take a look at ScottGu's post on code first and this EF post on conventions.
试试这个:(删除数据库,这样 EF 将创建新的)
Try this: (Remove database, so EF will create new)
这是您的一对一协会。
删除交换或书籍之间的一个引用,因此代码优先可以决定在您的一对一关系中哪一个更重要(书籍<-->交换)
如果您想知道原因,您应该阅读以下内容:
It's your one on one associations.
Remove one reference between exchange or book, so Code-first can decide which one is more important in your one on one relation (Book <--> Exchange)
If you want to know why, you should read this: