【问题标题】:EF 4.2 Code First. How to name a many-to-many table?EF 4.2 代码优先。如何命名多对多表?
【发布时间】:2012-01-12 16:19:43
【问题描述】:

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

例子:

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"));
}

【问题讨论】:

    标签: entity-framework entity-framework-4 ef-code-first


    【解决方案1】:

    也尝试映射列名。

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

    【讨论】:

      猜你喜欢
      • 2012-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-02
      • 2012-09-18
      • 1970-01-01
      相关资源
      最近更新 更多