Reputation: 3280
Let's say I have a EFF db model like this:
public class DbEFF
{
[Key]
public long Id { get; set; }
}
Now I'm creating a class where I'm inheriting from the db class like this:
public class DbTest:DbEFF
{
public DbTest(long id)
{
Id=id;
}
public string someotherproperty1 {get;set;}
}
Now I call the following code to write into the database:
var db = new DbEFF();
db.Id = "454545";
var model = new MasterEntities();
model.Table1.Add(db);
model.SaveChanges();
The weird thing now is that I get an inner exception saying that the column someotherproperty1 does not exist. What am I missing here? Why is the properties of the derived class being exposed like this?
The exception I'm getting is:
{"Invalid column name 'someotherproperty1'."}
Upvotes: 0
Views: 129
Reputation: 11568
In entity framework code first table per Hierarchy (TPH) is the default mapping.
This means that EF will map both DbTest and DbEFF to the same table. It will also add a column called Discriminator to see store what type of object is persisted (DbTest or DbEFF) in a particular row.
From the error you get it seems that your database already exist and that it has been created before you added the 'someotherproperty'. I.e. your table doesn't have 'someotherproperty' column.
To fix this there are several options, you need to get the schema of the table to match your classes or you must choose a different mapping strategy.
The easiest fix is to simply drop your database and let EF create the right database for you.
Upvotes: 1