开发者

Have Entity Framework ignore derived types

开发者 https://www.devze.com 2023-03-09 21:40 出处:网络
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 BackgroundTas

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>();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消