2014-02-27 60 views
2

我對現有數據庫使用Code First,並且必須映射到兩個表的實體,我該如何實現?如何將實體映射到多個表?

數據庫:

CREATE TABLE Supplier(
    SupplierId bigint identity(1,1) NOT NULL, 
    SupplierName varchar(60) NOT NULL 
) 

CREATE TABLE Customer(
    CustomerId bigint identity(1,1) NOT NULL, 
    CustomerName varchar(60) NOT NULL 
) 

CREATE TABLE SupplierPayments(
    SupplierPaymentNumber bigint NOT NULL, 
    SupplierId bigint NOT NULL, 
    PaymentValue decimal(19,4) NOT NULL 
) 

CREATE TABLE CustomerPayments(
    CustomerPaymentNumber bigint NOT NULL, 
    CustomerId bigint NOT NULL, 
    PaymentValue decimal(19,4) NOT NULL 
) 

ALTER TABLE SupplierPayments 
ADD CONSTRAINT PK_SupplierPayments 
    PRIMARY KEY CLUSTERED (SupplierPaymentNumber, SupplierId ASC); 

    ALTER TABLE CustomerPayments 
ADD CONSTRAINT PK_CustomerPaymentss 
    PRIMARY KEY CLUSTERED (CustomerPaymentNumber, CustomerId ASC); 

而且我的實體:

public partial class Supplier{ 
    public long SupplierId{ get; set; } 
    public string Name{ get; set; } 
    public virtual List<Payment> Payments{ get; set; } 
} 

public partial class Customer{ 
    public long CustomerId{ get; set; } 
    public string Name{ get; set; } 
    public virtual List<Payment> Payments{ get; set; } 
} 

public partial class Payment{ 
    public long PaymentNumber{ get; set; } 
    public decimal Value{ get; set; } 
} 

如何配置(用流利的API),以支付供應商和客戶之間的多到一的關係?

+0

沒有人可以幫助我嗎? –

回答

0

您可能會遇到麻煩維護這樣的代碼。這可能是明智的完全取消客戶和供應商的類,添加另一個名爲實體,可以說商業夥伴:

public partial class BusinessPartner { 
    public long Id {get; set; } 
    public string Name {get; set; } 
    public BusinessPartnerType Type {get; set; } 
} 

public enum BusinessPartnerType { 
    Customer = 1, 
    Supplier = 2 
} 

由於SupplierPayments和CustomerPayments的 - 這些人要與一個表被替換爲好,但你不知道必須指定付款類型,因爲它已在BusinessPartner表中指定。

相關問題