我正在使用Code first EF 6.1和DotConnect for oracle data provider和TPH。我创建了一个有2个子类的类。我正在尝试将记录添加到现有数据库表中。但是当我尝试保存更改时,它会抛出DbUpdateException。
异常:{“ORA-06550:第4行,第298行:\ nPL / SQL:ORA-00904:\”Discriminator \“:无效标识符\ nORA-06550:第4行,第1列:\ nPL / SQL: SQL语句忽略“}
这是我的代码:
Person.cs:-----------------
public class Person
{
public Person()
{
Departments= new HashSet<Department>();
}
[Key]
public decimal PersonId { get; set; }
public decimal OfficeId { get; set; }
public decimal PersonTypeId { get; set; }
public virtual Office Office{ get; set; }
public virtual ICollection<Department> Departments{ get; set; }
protected bool success = false;
public virtual bool FindDepartment(int personType)
{
return success;
}
public class PersonHR: Person
{
public override bool FindDepartment(int personType)
{
PersonEntityModel dbcontext = new PersonEntityModel ();
Person p = new Person ();
p.OfficeId= 765;
p.PersonTypeId = personType;
try
{
dbcontext.Persons.Add(p);
dbcontext.SaveChanges();
decimal personId= p.PersonId;
}
catch (DbEntityValidationException dbEx)
{
}
return success;//this is just a test method so don't worry about return.
}
}
public class PersonIT : Person
{
public override bool FindDepartment(int personType)
{
return success;
}
}
}
PersonEntityModel.cs -------------------
public partial class PersonEntityModel: DbContext
{
public PersonEntityModel()
: base("name=PersonEntityModel")
{
}
public virtual DbSet<Office> Offices{ get; set; }
public virtual DbSet<Person> Persons{ get; set; }
public virtual DbSet<Department> Departments{ get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new PersonMap());
}
}
public class PersonMap: EntityTypeConfiguration<Person>
{
public PersonMap()
{
this.Property(p => p.PersonId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
this.HasMany(p => p.Departments).WithRequired(d=> d.Person).HasForeignKey(d=> d.PersonId);
//table & column mappings
this.ToTable("TABLEPERSON");
this.Property(p => p.PersonId).HasColumnName("PERSONID");
this.Property(p => p.OfficeId).HasColumnName("OFFICEID");
this.Property(p => p.PersonTypeId).HasColumnName("PERSONTYPEID");
//this.Map<DomainObjectModel.ObjectModel.Person.PersonHR>(m => m.Requires("TYPE").HasValue(11));
//this.Map<DomainObjectModel.ObjectModel.Person.PersonIT>(m => m.Requires("TYPE").HasValue(22));
}
}
没有最后2行,它会引发异常:{“ORA-06550:第4行,第298行:\ nPL / SQL:ORA-00904:\”Discriminator \“:无效标识符\ nORA-06550:第4行,第1列:\ nPL / SQL:SQL语句忽略“}
使用最后2行,它会引发此异常:{“ORA-06550:第4行,第298行:\ nPL / SQL:ORA-00904:\”TYPE \“:无效标识符\ nORA-06550:第4行,第1列:\ nPL / SQL:SQL语句忽略“}
我是TPH的新手。如何解决上述问题?
另外,请给我发一些关于TPH的好文章。我已阅读此http://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph。
感谢您的帮助,