EF 4.2 Code First。如何为多对多表命名?

4

在多对多关系中,是否可以指定表名称?

例如:

class A
{
    public int Id { get; set; }
    // .....

    public virtual ICollection<B> BCollection { get; set; }
}

class B
{
    public int Id { get; set; }
    // .....

    public virtual ICollection<A> ACollection { get; set; }
}

我曾认为这可能有效,但显然并不是。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<A>().HasMany(x => x.BCollection).WithMany(y => y.ACollection).Map(m => m.ToTable("My_Custom_Name"));
}
1个回答

12

尝试将列名也映射一下。

        modelBuilder.Entity<A>()
        .HasMany(x => x.BCollection).WithMany(y => y.ACollection)
            .Map(m =>
            {
                m.ToTable("My_Custom_Name");
                m.MapLeftKey("AId");
                m.MapRightKey("BId");
            });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接