开发者

How do I rename columns for a many to many table with composite keys using EF CTP5 code-first?

开发者 https://www.devze.com 2023-02-05 07:59 出处:网络
Here is the sample from the ADO.NET site. Rename columns in many:many table: modelBuilder.Entity<Product>()

Here is the sample from the ADO.NET site.

Rename columns in many:many table:

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

Please extend this ex开发者_如何学运维ample with a second imaginary key (i.e. ProductId2, TagId2) on each of the tables.


For that you just need to map the secondary columns as well:

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

精彩评论

暂无评论...
验证码 换一张
取 消