EF 4.1 RC Many to many relationship in EF CF

后端 未结 2 1033
滥情空心
滥情空心 2021-02-10 09:43

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 ICol         


        
2条回答
  •  甜味超标
    2021-02-10 10:22

    This was problematic with EDMX but with EF 4.1 fluent API you can map it:

    modelBuilder.Entity()
                .HasMany(b => b.authors)
                .WithMany(a => a.books)
                .Map(m => m.MapLeftKey("BookID")
                       .MapRightKey("AuthorID")
                       .ToTable("BookAuthor"));
    

    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.

提交回复
热议问题