Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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

like image 661
bitfrickler Avatar asked Jun 01 '11 07:06

bitfrickler


1 Answers

Try to use this in your derived context:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    modelBuilder.Ignore<UserAccountTask>();
    modelBuilder.Ignore<BackgroundTask>();
}
like image 176
Ladislav Mrnka Avatar answered Sep 23 '22 17:09

Ladislav Mrnka