EF 4.1 RC EF CF 中的多对多关系
我有两个具有多对多关系的实体,如下所示:
class author
{
public int AuthorID{get;set;}
public string Name{get;set;}
public virtual ICollection<book> books{get;set;}
}
class book
{
public int BookID{get;set;}
public string Name{get;set;}
public virtual ICollection<author> authors{get;set;}
}
并且我有一个名为 Bookauthor 的中间表,定义如下:
BookAuthor: int
int AuthorID
int BookID
如何使用 FluentAPI 映射此表
谢谢!
I have two entities with many to many relationship like this:
class author
{
public int AuthorID{get;set;}
public string Name{get;set;}
public virtual ICollection<book> books{get;set;}
}
class book
{
public int BookID{get;set;}
public string Name{get;set;}
public virtual ICollection<author> authors{get;set;}
}
and I have an intermediate table named Bookauthor defined like this:
BookAuthor: int
int AuthorID
int BookID
How to map this using FluentAPI
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这在 EDMX 中是有问题的,但在 EF 4.1 Fluent API 中您可以映射它:
如您所见,我没有映射
BookAuthor
列。该列对于 EF 来说是未知的,并且必须自动递增。这显然不适用于代码优先方法,但前提是您对现有数据库使用 Fluent API。
This was problematic with EDMX but with EF 4.1 fluent API you can map it:
As you can see I don't map
BookAuthor
column. That column is unknown to EF and must be auto incremented.This obviously can't work with a Code-first approach but only if you use Fluent API against existing database.
我不认为 EF 允许您在多对多连接表中拥有单独的 Id。
另一种方法是将 BookAuthor 映射为实体。
顺便说一句,NHibernate 使用 idbag
I don't think EF allows you to have a separate Id in many-to-many junction tables.
The alternative is just mapping BookAuthor as an entity.
On a side note, NHibernate supports this construct using an idbag