entity-framework - Entity Framework (CTP5、Fluent API)。重命名导航属性列

标签 entity-framework code-first

我有两个实体:

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

生成数据库时,我的“Customer”表有两列用于“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");
        });            
}

alt text

关于entity-framework - Entity Framework (CTP5、Fluent API)。重命名导航属性列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4660908/

相关文章:

c# - EF4 代码优先 CTP5 多对一

c# - EF 代码优先 : Include not working on optional relationship

c# - 需要使用Seed方法向映射表添加数据吗?

c# - 如何动态构建 Entity Framework 查询?

c# - 使用 MySql 在 Entity Framework 6 中延迟加载数据不起作用

c# - 使用 Distinct 和 Order By 时 Entity Framework 中的多个查询和性能不佳

c# - 什么是 EF 默认 ID 命名约定代码优先?

entity-framework - EF 4.2 忽略代码中的单个属性优先映射到复杂类型

sql-server - 两个不同列的 SUM 和 DISTINCT

entity-framework - 何时在 Entity Framework 上使用 t-SQL