【问题标题】:How do I rename columns for a many to many table with composite keys using EF CTP5 code-first?如何使用 EF CTP5 代码优先重命名具有复合键的多对多表的列?
【发布时间】:2011-01-19 19:17:09
【问题描述】:

这是来自 ADO.NET 站点的示例。

重命名 many:many 表中的列:

modelBuilder.Entity<Product>() 
.HasMany(p => p.Tags)
.WithMany(t => t.Products)
.Map(m =>
    {
        m.MapLeftKey(p => p.ProductId, "CustomFkToProductId");
        m.MapRightKey(t => t.TagId, "CustomFkToTagId");
    });

请在每个表上使用第二个假想键(即 ProductId2、TagId2)扩展此示例。

【问题讨论】:

    标签: many-to-many code-first entity-framework-ctp5


    【解决方案1】:

    为此,您也只需要映射辅助列:

    modelBuilder.Entity<Product>()
                .HasMany(p => p.Tags)
                .WithMany(t => t.Products) 
                .Map(m => 
                { 
                    m.MapLeftKey(p => p.ProductId, "CustomFkToProductId");
                    m.MapLeftKey(p => p.ProductId2, "CustomFkToProductId2");
                    m.MapRightKey(t => t.TagId, "CustomFkToTagId"); 
                    m.MapRightKey(t => t.TagId2, "CustomFkToTagId2"); 
                });
    

    【讨论】:

    • @Morteza-Manavi 我试过了。我收到以下错误 Number of Properties in the Dependent and Principal Role in a relationship constraint must be exactly identical. 我的 Many:Many 表的两边都有 3 列
    • @Vamsi Krishna Rallabhandi:您能否在新线程中展示您的代码?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 2012-09-18
    相关资源
    最近更新 更多