在EF4 Code First中创建多对多关系

时间:2011-12-19 03:11:09

标签: asp.net-mvc-3 entity-framework-4 ef-code-first

我正在使用EF4 Code-First进行ASP.NET MVC项目的建模。我有以下模型类:

public class ComicBook
{
    public long ComicBookID { get; set; }
    public string IssueTitle { get; set; }
    public int IssueNumber { get; set; }
    public DateTime PublishDate { get; set; }

    public IList<ComicBookPerson> Writers { get; set; }
    public IList<ComicBookPerson> Pencillers { get; set; }

    public IList<User> CollectingUsers { get; set; }
}

public class ComicBookPerson
{
    public long ComicBookPersonID { get; set; }
    public string Name { get; set; }

    public IList<ComicBook> WorkedOnComicBooks { get; set; }
}

public class User
{
    [Key]
    public long UserID { get; set; }
    public string Email { get; set; }

    public IList<ComicBook> CollectedBooks { get; set; }
}

以下DbContext:

public class ComicContext : DbContext
{
    public DbSet<ComicBook> ComicBooks { get; set; }
    public DbSet<ComicBookPerson> ComicBookPerson { get; set; }
    public DbSet<User> Users { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ComicBook>()
            .HasMany(b => b.Writers)
            .WithMany(w => w.WorkedOnComicBooks)
            .Map(t => t.MapLeftKey("ComicBookID")
                .MapRightKey("WriterID")
                .ToTable("ComicBook_Writers"));

        modelBuilder.Entity<ComicBook>()
            .HasMany(b => b.Pencillers)
            .WithMany(p => p.WorkedOnComicBooks)
            .Map(t => t.MapLeftKey("ComicBookID")
                .MapRightKey("PencillerID")
                .ToTable("ComicBook_Penciller"));
    }
}

需要应用的规则是:

  • 用户可以在其收藏中包含许多漫画书
  • 漫画书可以在许多用户的收藏中
  • 每本漫画书可以有一个或多个作家
  • 每本漫画书可以有一个或多个Pencillers
  • ComicBookPerson可以是任何书籍上的Writer或Penciller
  • ComicBookPerson可以作为Writer,Penciller或两者兼用的书籍

用户和漫画书之间的多对多架构创建得很好。 ComicBooks和ComicBookPersons之间的多对多关系正在打破。我得到的错误信息是:

Schema specified is not valid. Errors: 
(15,6) : error 0040: Type ComicBook_Writers is not defined in namespace Comics.Models (Alias=Self).

我基本上想要两个连接表,一个叫做ComicBook_Writers,另一个叫做ComicBookPencillers。两个表都包含ComicBookID和ComicBookPersonID。这在EF Code First中是否可行?

2 个答案:

答案 0 :(得分:2)

您需要ComicBookPerson类中与作家和铅笔有关的两个集合属性。

public class ComicBookPerson
{
    public long ComicBookPersonID { get; set; }
    public string Name { get; set; }

    public IList<ComicBook> WroteComicBooks { get; set; }
    public IList<ComicBook> PenciledComicBooks { get; set; }
}

模型映射为

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<ComicBook>()
        .HasMany(b => b.Writers)
        .WithMany(w => w.WroteComicBooks)
        .Map(t => t.MapLeftKey("ComicBookID")
            .MapRightKey("WriterID")
            .ToTable("ComicBook_Writers"));

    modelBuilder.Entity<ComicBook>()
        .HasMany(b => b.Pencillers)
        .WithMany(p => p.PenciledComicBooks)
        .Map(t => t.MapLeftKey("ComicBookID")
            .MapRightKey("PencillerID")
            .ToTable("ComicBook_Penciller"));
}

答案 1 :(得分:0)

我认为这种模式接近正确,但仍然缺少某些东西 ComicBookPerson可以是作家和施法者,这意味着您仍然需要在班级ComicBookPerson中使用第三个列表:

public IList<ComicBook> WroteComicBooks { get; set; }
public IList<ComicBook> PenciledComicBooks { get; set; }
public IList<ComicBook> WrotPencComicBooks { get; set; }

我是对的吗?