2015-08-29 38 views
1

是否可以與列名不匹配的具有複合鍵的兩個實體具有父/子關係?實體框架 - 與非匹配列的父子關係

例如。 表A的複合鍵字段(CustNmbr,SITEID) 表B的複合鍵字段(帳號,SiteNumber)

我已經試過各種映射似乎並不能使它發揮作用。根據我讀過的內容,您應該能夠在模型中將其映射出來。我試着和相當悲慘地失敗了:

public DbSet<Customer> Customers { get; set; } 
    public DbSet<Customer_Contract_Data> Contracts { get; set; } 


    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     // Configure the primary Key for the OfficeAssignment 
     modelBuilder.Entity<Customer>() 
      .HasKey(t => new { t.CustNmbr, t.SiteId }); 

     modelBuilder.Entity<Customer>() 
      .HasRequired(t => t.Contracts) 
      .WithMany() 
      .HasForeignKey(t => new { t.CustNmbr, t.SiteId }); 
    } 


    // function that retursn all orders 
    private IQueryable<Customer> getCustomers() 
    { 
     // return the data 
     return Customers; 
    } 

    public List<Customer> GetCustomers() 
    { 
     return getCustomers().ToList(); 
    } 

    // function that retursn all orders 
    public Customer GetCustomer(Int32 siteId, string custNbr) 
    { 
     // return the data 
     return getCustomers().Include(m=>m.Contracts).FirstOrDefault(x => x.CustNmbr == custNbr && x.SiteId == siteId); ; 
    } 

    // dispose 
    protected override void Dispose(bool disposing) 
    { 
     base.Dispose(disposing); 
    } 
} 

這裏有階級結構

[Table("CustomerData")] 
public class Customer 
{ 
    [Key, Column(Order=2)] 
    public string CustNmbr { get; set; } 

    [Key, Column(Order=1)] 
    public Int32 SiteId { get; set; } 

    public string GPCompany { get; set; } 
    public string CustName { get; set; } 
    public string Address1 { get; set; } 
    public string Address2 { get; set; } 
    public string Address3 { get; set; } 
    public string City { get; set; } 
    public string State { get; set; } 
    public string Country { get; set; } 
    public string CCode { get; set; } 
    public string Zip { get; set; } 
    public string Phone1 { get; set; } 
    public string Fax { get; set; } 
    public DateTime DateUploadedFromLive { get; set; } 

    public virtual ICollection<Customer_Contract_Data> Contracts { get; set; } 

    public Customer() { } 

} 

[Table("Customer_Contract_Data")] 
public class Customer_Contract_Data 
{ 
    [Key] 
    public string Contractnumber { get; set; } 

    public Int32 SiteNumber { get; set; } 
    public string Account { get; set; } 
    public string Contracttype { get; set; } 
    public string Contracttypedescription { get; set; } 
    public string Servicetype { get; set; } 
    public string Country { get; set; } 
    public string Zipcode { get; set; } 
    public DateTime Contractstartdate { get; set; } 
    public DateTime Contractenddate { get; set; } 

    public virtual Customer Customer { get; set; } 

} 

回答

0

當然, 看看下面的代碼(我簡化您的實體):

映射:

modelBuilder.Entity<Customer>() 
    .HasKey(t => new { t.CustomerNumber, t.SiteId }) //composite key 
    .HasMany(t => t.Contracts) 
    .WithRequired(t => t.Customer); 

modelBuilder.Entity<ContractData>() // child entity 
    .HasKey(c => new { c.ContractNumber, c.Account }) // composite key 
    .HasRequired(c => c.Customer) // parent relation 
    .WithMany() 
    .Map(c => c.MapKey("column1", "column2")); //mapping to different column names 

實體:

public class ContractData 
{ 
    public virtual string ContractNumber { get; set; } 
    public virtual string Account { get; set; } 
    public virtual Customer Customer { get; set; } 
} 

public class Customer 
{ 
    public Customer() 
    { 
     Contracts = new List<ContractData>(); 
    } 

    public virtual string CustomerNumber { get; set; } 
    public virtual int SiteId { get; set; } 
    public virtual ICollection<ContractData> Contracts { get; set; } 
    public virtual string Name { get; set; } 
} 

底層DB:

Customer   1 <---> n ContractData 
-----------------    ----------------- 
CustomerNumber PK    column1  FK 
SiteId   PK    column2  FK 
Name       ContractNumber PK 
           Account  PK 
+0

非常感謝你的回覆。我在這一塊上徘徊了一下。這看起來不錯。乾杯! –

+0

不客氣。試試看,讓我知道如果它是正確的。 – Tomas