EF 4.1 RC Many to many relationship in EF CF

后端 未结 2 1032
滥情空心
滥情空心 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<book>()
                .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.

    0 讨论(0)
  • 2021-02-10 10:27

    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

    0 讨论(0)
提交回复
热议问题