I am using Code first EF 6.1 with DotConnect for oracle data provider and TPH. I have created one class which has 2 subclasses. I am trying to add a record into existing database table. But it throws a DbUpdateException when I try to savechanges.
Exception: {"ORA-06550: line 4, column 298:\nPL/SQL: ORA-00904: \"Discriminator\": invalid identifier\nORA-06550: line 4, column 1:\nPL/SQL: SQL Statement ignored"}
Here is my code:
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));
}
}
Without last 2 lines, it throws an exception:{"ORA-06550: line 4, column 298:\nPL/SQL: ORA-00904: \"Discriminator\": invalid identifier\nORA-06550: line 4, column 1:\nPL/SQL: SQL Statement ignored"}
With last 2 lines, it throws this exception: {"ORA-06550: line 4, column 298:\nPL/SQL: ORA-00904: \"TYPE\": invalid identifier\nORA-06550: line 4, column 1:\nPL/SQL: SQL Statement ignored"}
I am new to TPH. How to resolve above issues?
Also, please send me some good article to read about TPH. I have read this http://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.
Appreciate your help,