简体   繁体   中英

Entity Framework 4 Code First Fluent API Configuration for One-to-One Relationship with Inheritance

I'm trying to implement the "Party" domain pattern using EF Code first with Fluent Configuration for all my business classes. The configuration is driving me to drink (not a bad thing necessarily but next stop is the cliff)

What I would like to have happen is something like the code shown below. The requirements for the database are that I end up with the following tables:

A separate "Parties" Table A separate "People" Table An separate "Organizations" Table

The People Table has a required One-To-One relationship with the Parties Table, ie you must have a Party before you have a Person.

The Organization Table has a required One-To-One relationship with the Parties Table, ie you must have a Party before you have an Organization.

And, all business objects inherit from a BusinessObject base class that should Not end up in the database, ie property inheritence only.

I want to use a Code First / Fluent API approach using "EntityTypeConfiguration" classes ie I have the following classes for configuration:

  1. A Context Class for settup up configuration.
  2. Individual configuration classes for each business object ie PersonConfiguration, OrganizationConfiguration, etc.

Can anyone please help me with how to define Fluent Configuration for this scenario? Or point me to an example?

Thanks! (Code Below)

PSEUDO CODE...................................................................

// 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 { }
  }
}

Pretty tough question.
I think what you're looking for is implementing TPT (Table Per Type) inheritance. I suspect some of your requirements will not be satisfied by EF. For more information visit the following links:
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM