I'm trying to map the AdventureWorks 2012 sample database to an EF 6.1.3 code-first data layer, and am stuck at how to map the Employee
and Person
entities. Employee should apparently derive from Person
, with a Person.PersonType
of EM
, but I don't know how to map this using EntityTypeConfiguration<TEntity>
'mapping' classes. Knowing this, I could also map Person
to derive from BusinessEntity
.
-
Employee should derive from Person and you have to give them the same PK (let Employee use the inherited PersonID Key). EF will correctly map them.– Emmanuel M.Apr 27, 2015 at 10:14
-
weblogs.asp.net/manavi/… and related posts– CAD blokeJul 6, 2017 at 22:58
1 Answer
I will explain how to perform the mapping between the Employee
and the Person
tables using Code First approach, you can follow the same procedure to map inheritance between BusinessEntity
and Person
.
The inheritance mapping strategy used is TPT (Table Per Type), I've created a simple Console Application, With the AdventureWorks2012 Database installed, I followed the EF DataModel Wizard to generate the code first classes that I will modify to map the inheritance, So here is the resulted code:
The Person class:
public partial class Person
{
[Key]
public int BusinessEntityID { get; set; }
[Required]
public string PersonType { get; set; }
public bool NameStyle { get; set; }
public string Title { get; set; }
[Required]
public string FirstName { get; set; }
public string MiddleName { get; set; }
[Required]
public string LastName { get; set; }
public string Suffix { get; set; }
public int EmailPromotion { get; set; }
[Column(TypeName = "xml")]
public string AdditionalContactInfo { get; set; }
[Column(TypeName = "xml")]
public string Demographics { get; set; }
}
The Employee class:
public partial class Employee: Person
{
[Required]
public string NationalIDNumber { get; set; }
[Required]
public string LoginID { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public short? OrganizationLevel { get; set; }
[Required]
public string JobTitle { get; set; }
[Column(TypeName = "date")]
public DateTime BirthDate { get; set; }
[Required]
public string MaritalStatus { get; set; }
[Required]
public string Gender { get; set; }
[Column(TypeName = "date")]
public DateTime HireDate { get; set; }
public bool SalariedFlag { get; set; }
public short VacationHours { get; set; }
public short SickLeaveHours { get; set; }
public bool CurrentFlag { get; set; }
public Guid rowguid { get; set; }
public DateTime ModifiedDate { get; set; }
}
And Finally the AW Context Class:
public partial class AW : DbContext
{
public AW()
: base("name=AWConnectionString")
{
}
public virtual DbSet<Employee> Employees { get; set; }
public virtual DbSet<Person> People { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Person>().ToTable("Person.Person");
modelBuilder.Entity<Employee>().ToTable("HumanResources.Employee");
}
}
A simple test (That works for me ;) ):
class Program
{
static void Main(string[] args)
{
using(var db= new AW())
{
var e = db.Employees.First();
e.JobTitle = "Web Developper";
db.SaveChanges();
}
}
}
You can refer to this article for more details
-
One thing I don't see here is how to tell EF that
PersonType
is a 'differentiator` column. We could simply hard set the value 'EM' in theEmployee
ctor, but I somehow thought there was more to this aspect of the mapping.– ProfKMay 5, 2015 at 4:52 -
by 'differentiator' you mean the 'descriminator' which is used in the TPH inheritance strategy. I haven't specified a discriminator above because I used TPT to map inheretance. May 5, 2015 at 10:09
-
Yes, that is the term I was looking for. So I suppose here we can merely use the ctor of
Employee
to set that.– ProfKMay 5, 2015 at 13:45 -
Yeah this may work for the
Employee
class, but checking the values that thePersonType
column takes (EM,SC,VC,IN, ..etc), from that I don't think there is a corresponding table/class for each value (EM=>Employee, VC=>??). So I think this column has nothing to do with inheretance mapping! May 5, 2015 at 13:53