如何在EF代码中首先映射继承的实体 [英] How to map inherited entities in EF code-first
问题描述
Employee
和个人
实体。员工应该明显地从 Person
中导出, Person.PersonType
EM
,但我不知道如何使用 EntityTypeConfiguration< TEntity>
'映射'类映射。知道这一点,我也可以映射 Person
从 BusinessEntity
。我将解释如何执行 Employee
和 Person
表使用Code First方法,您可以按照相同的过程在 BusinessEntity
和 Person
之间映射继承。
使用的继承映射策略是TPT(Table Per Type),我创建了一个简单的控制台应用程序,安装了AdventureWorks2012数据库后,我按照EF DataModel向导生成我将修改的代码第一个类来映射继承,所以这里是结果代码:
Person类:
public partial class Person
{
[Key]
public int BusinessEntityID {get;组;
[必需]
public string PersonType {get;组; }
public bool NameStyle {get;组; }
public string标题{get;组;
[必需]
public string FirstName {get;组; }
public string MiddleName {get;组;
[必需]
public string LastName {get;组; }
public string Suffix {get;组; }
public int EmailPromotion {get;组; }
[Column(TypeName =xml)]
public string AdditionalContactInfo {get;组; }
[Column(TypeName =xml)]
public string Demographics {get;组;
}
员工类:
public partial class Employee:Person
{
[必需]
public string NationalIDNumber {get;组;
[必需]
public string LoginID {get;组; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public short? OrganizationLevel {get;组;
[必需]
public string JobTitle {get;组; }
[Column(TypeName =date)]
public DateTime BirthDate {get;组;
[必需]
public string MaritalStatus {get;组;
[必需]
public string Gender {get;组; }
[Column(TypeName =date)]
public DateTime HireDate {get;组; }
public bool SalariedFlag {get;组; }
public short VacationHours {get;组; }
public short SickLeaveHours {get;组; }
public bool CurrentFlag {get;组; }
public Guid rowguid {get;组; }
public DateTime ModifiedDate {get;组;
}
最后,AW上下文类:
public partial class AW:DbContext
{
public AW()
:base(name = AWConnectionString)
{
}
public virtual DbSet< Employee>员工{get;组; }
public virtual DbSet< Person>人{get;组;
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity< Person>()。ToTable(Person.Person);
modelBuilder.Entity< Employee>()。ToTable(HumanResources.Employee);
}
}
一个简单的测试(对我有用) :
class Program
{
static void Main(string [] args)
{
使用(var db = new AW())
{
var e = db.Employees.First();
e.JobTitle =Web Developper;
db.SaveChanges();
}
}
}
您可以参考这篇文章了解更多细节
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
.
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
这篇关于如何在EF代码中首先映射继承的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!