Have Entity Framework ignore derived types
in my current project I'm using code first approach.
I have a type called Task which is part of the model. I also have BackgroundTask derived from Task and UserAccountTask derived from BackgroundTask.
When I simply try to create an object of type Task and add it to my task repository, I get a DbUpdateException as soon as I try to save the changes to the db. Its inner exception states:
"Invalid column 开发者_Go百科name 'UserAccount_UserId'.\r\nInvalid column name 'UserAccount_Lastname'.\r\nInvalid column name 'UserAccount_Firstname'.\r\nInvalid column name 'UserAccount_Fullname'.\r\nInvalid column name 'UserAccount_Password'.\r\nInvalid column name 'UserAccount_Title'[...]"
UserAccount is another type and a property of UserAccountTask (UserId, Lastname etc. are properties of UserAccount).
I hope my description of the problem isn't too messed up :-/
I just want EF to ignore the fact that Task is the base class for some other type because IMHO it doesn't matter at that time.
Thanks in advance, Kevin
Try to use this in your derived context:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Ignore<UserAccountTask>();
modelBuilder.Ignore<BackgroundTask>();
}
加载中,请稍侯......
精彩评论