gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:37:27 25 4
gpt4 key购买 nike

我有两个实体:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com