【发布时间】:2012-01-20 19:21:37
【问题描述】:
我正在尝试首先使用 EF Code 和 Fluent Configuration 为我的所有业务类实现“Party”域模式。配置让我喝酒(不一定是坏事但下一站是悬崖)
我希望发生的事情类似于下面显示的代码。对数据库的要求是我最终得到以下表格:
单独的“Parties”表 单独的“人员”表 单独的“组织”表
People 表与 Party 表具有必需的一对一关系,即您必须先拥有 Party,然后才能拥有 Person。
Organization Table 与 Party Table 具有必需的一对一关系,即您必须先拥有 Party,然后才能拥有 Organization。
而且,所有业务对象都继承自一个不应在数据库中结束的 BusinessObject 基类,即仅属性继承。
我想通过“EntityTypeConfiguration”类使用 Code First / Fluent API 方法,即我有以下配置类:
- 设置配置的上下文类。
- 每个业务对象的单独配置类,即 PersonConfiguration、OrganizationConfiguration 等。
任何人都可以帮助我如何为这种情况定义流畅的配置吗?或者给我举个例子?
谢谢! (代码如下)
伪代码.......................... ......................
// Business Obsject Base Class
// Should not be implemented as a database table!!!!
// No Primary Key Needed Here
public abstract class BusinessObject
{
private List<BusinessRule> _businessRules = new List<BusinessRule>();
public List<BusinessRule> BusinessRules
{
get { return _businessRules; }
}
private List<string> _validationErrors = new List<string>();
public List<string> ValidationErrors
{
get { return _validationErrors; }
}
public DateTime CreatedDate { get; set; }
public Person CreatedBy { get; set; }
public ModifiedDate { get; set; }
public Byte[] RowVersion { get; set; }
}
// Party Pattern Base Class inherits BusinessObject base class
// Shared Basis for People and Organizations
public abstract class Party : BusinessObject
{
public virtual int PartyId { get; set; }
public abstract string DisplayName { get; internal set; }
}
// Is a Party, Implements both BusinessObject base and Party base
public class Person : Party
{
// Both a Primary and a Foreign Key
// Should be a one-to-one relationship with Party
//would like this to be "PersonId" not "PartyId" but it's OK if it is not
public int PersonId { get; set; }
pubilc virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public override string DisplayName
{
get { return LastName + ", " + FirstName; }
internal set { }
}
}
// Is a Party, Implements both BusinessObject base and Party base
public class Organization : Party
{
// Both a Primary and a Foreign Key
// Should be a one-to-one relationship with Party
//would like this to be "PersonId" not "PartyId" but it's OK if it is not
public int OrganizationId { get; set; }
pubilc virtual string Name { get; set; }
public override string DisplayName
{
get { return Name; }
internal set { }
}
}
【问题讨论】: