If Entity Framework ignores derived types

In my current project, I am using the first code approach.

I have a type called Task , which is part of the model. I also have a BackgroundTask derived from Task and a UserAccountTask derived from BackgroundTask .

When I just 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 db. Its internal exception:

"Invalid column name is' UserAccount_UserId '. \ R \ nInvalid column name is' UserAccount_Lastname'. \ R \ nInvalid column name is' UserAccount_Firstname '. \ R \ nInvalid column name is' UserAccount_Fullname'. \ R \ nInvalid column name is' UserAccount. \ nInvalid column name 'UserAccount_Title' [...] "

UserAccount is a different type, and the UserAccountTask property ( UserId , Lastname , etc. - these are UserAccount properties).

I hope my description of the problem is not too confusing: - / I just want EF to ignore the fact that Task is a base class for another type, because IMHO doesn't matter at that time.

Thanks in advance, Kevin

+6
source share
1 answer

Try using this in a derived context:

 protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Ignore<UserAccountTask>(); modelBuilder.Ignore<BackgroundTask>(); } 
+15
source

Source: https://habr.com/ru/post/889515/


All Articles