C# 如何将一个实体映射到多个表?
我首先使用代码到一个现有的数据库,并且有一个实体我必须映射到两个表,我如何才能做到这一点 数据库:C# 如何将一个实体映射到多个表?,c#,entity-framework,C#,Entity Framework,我首先使用代码到一个现有的数据库,并且有一个实体我必须映射到两个表,我如何才能做到这一点 数据库: 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 varc
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; }
}
公共部分类供应商{
公共长供应商ID{get;set;}
公共字符串名称{get;set;}
公共虚拟列表付款{get;set;}
}
公共部分类客户{
公共长CustomerId{get;set;}
公共字符串名称{get;set;}
公共虚拟列表付款{get;set;}
}
公共部分类付款{
公共长PaymentNumber{get;set;}
公共十进制值{get;set;}
}
如何配置(使用Fluent API)供应商和客户之间的多对一付款关系?维护此类代码可能会遇到问题。更明智的做法是删除客户和供应商类,并添加另一个名为“业务伙伴”的实体:
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
}
对于供应商付款和客户付款-这些也应替换为一个表,但您不必指定付款类型,因为它已在业务伙伴表中指定