2011-01-11 68 views
7

的專欄中,我有兩個的entites:實體框架(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'?

回答

4

基本上你想在獨立相關定製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

+0

莫爾塔扎,再次感謝。 – 2011-01-12 08:13:48