我将 Code first EF 6.1 与 DotConnect 一起用于 Oracle 数据提供程序和 TPH。我创建了一个有 2 个子类的类。我正在尝试将记录添加到现有的数据库表中。但是当我尝试保存更改时它会引发 DbUpdateException。
异常:{“ORA-06550:第 4 行,第 298 列:\nPL/SQL:ORA-00904:\“鉴别器\”:无效标识符\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: line 4, column 298:\nPL/SQL: ORA-00904: \"Discriminator\": invalid identifier\nORA-06550: line 4, column 1: \nPL/SQL: SQL 语句被忽略"}
对于最后 2 行,它会引发此异常: {"ORA-06550: line 4, column 298:\nPL/SQL: ORA-00904: \"TYPE\": invalid identifier\nORA-06550: line 4, column 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。
感谢你的帮助,