【问题标题】:Entity Framework 4 Code First Fluent API Configuration for One-to-One Relationship with InheritanceEntity Framework 4 Code First Fluent API 配置与继承的一对一关系
【发布时间】: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 方法,即我有以下配置类:

  1. 设置配置的上下文类。
  2. 每个业务对象的单独配置类,即 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 { }
  }
}

【问题讨论】:

    标签: entity-framework fluent


    【解决方案1】:

    相当棘手的问题。
    我认为您正在寻找的是实现 TPT(每种类型的表)继承。我怀疑 EF 不会满足您的某些要求。欲了解更多信息,请访问以下链接:
    Entity Framework + Multilevel Inheritance + EF Code first http://weblogs.asp.net/manavi/archive/2010/12/28/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt.aspx

    【讨论】:

      猜你喜欢
      • 2016-06-08
      • 1970-01-01
      • 1970-01-01
      • 2015-05-11
      • 2013-01-26
      • 1970-01-01
      • 2016-06-01
      • 1970-01-01
      • 2013-01-12
      相关资源
      最近更新 更多