本文介绍了EF 4.1 RC EF CF中的许多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个实体有这么多的关系:
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;}
}
我有一个中间表名为Bookauthor的表如下定义:
and I have an intermediate table named Bookauthor defined like this:
BookAuthor: int
int AuthorID
int BookID
如何使用FluentAPI进行映射
How to map this using FluentAPI
谢谢!!
推荐答案
这与EDMX有问题,但EF 4.1流畅的API 可以映射:
This was problematic with EDMX but with EF 4.1 fluent API you can map it:
modelBuilder.Entity<book>()
.HasMany(b => b.authors)
.WithMany(a => a.books)
.Map(m => m.MapLeftKey("BookID")
.MapRightKey("AuthorID")
.ToTable("BookAuthor"));
正如您所看到的,我不会映射 BookAuthor
列。该列对于EF是未知的,必须自动递增。
As you can see I don't map BookAuthor
column. That column is unknown to EF and must be auto incremented.
这显然无法使用Code-first方法,但只有当您使用Fluent API与现有数据库时。
This obviously can't work with a Code-first approach but only if you use Fluent API against existing database.
这篇关于EF 4.1 RC EF CF中的许多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!