实体框架(CTP5,Fluent API)。 重命名导航属性的列

我有两个实体:

public class Address
{
        public int Id { get; set; }
 public string FirstName { get; set; 
 public string LastName { get; set; }
}

public partial class Customer
    {
        public int Id { get; set; }
        public string Email { get; set; }
        public string Username { get; set; }

        public virtual Address BillingAddress { get; set; }
        public virtual Address ShippingAddress { get; set; }
    }

以下是映射类:

public partial class AddressMap : EntityTypeConfiguration<Address>
    {
        public AddressMap()
        {
            this.ToTable("Addresses");
            this.HasKey(a => a.Id);
        }
    }
public partial class CustomerMap : EntityTypeConfiguration<Customer>
    {
        public CustomerMap()
        {
            this.ToTable("Customer");
            this.HasKey(c => c.Id);

            this.HasOptional<Address>(c => c.BillingAddress);
            this.HasOptional<Address>(c => c.ShippingAddress);
        }
    }

当生成数据库时,我的'客户'表有两个'BillingAddress'和'ShippingAddress'属性列。 他们的名字是'AddressId'和'AddressId1'。 问题:我如何将它们重命名为'BillingAddressId'和'ShippingAddressId'?


基本上你想在一个独立的关联中定制FK列名,这段代码会为你做这件事:

public CustomerMap()
{
    this.ToTable("Customer");            

    this.HasOptional<Address>(c => c.BillingAddress)
        .WithMany()
        .IsIndependent().Map(m => 
        {
            m.MapKey(a => a.Id, "BillingAddressId");                    
        });

    this.HasOptional<Address>(c => c.ShippingAddress)
        .WithMany()
        .IsIndependent().Map(m =>
        {
            m.MapKey(a => a.Id, "ShippingAddressId");
        });            
}

替代文字

链接地址: http://www.djcxy.com/p/49905.html

上一篇: Entity framework (CTP5, Fluent API). Rename column of navigation property

下一篇: How to fix "file not found" on git svn dcommit?