让实体框架忽略派生类型 [英] Have Entity Framework ignore derived types

查看:87
本文介绍了让实体框架忽略派生类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我当前的项目中,我正在使用代码优先方法.

in my current project I'm using code first approach.

我有一个名为Task的类型,它是模型的一部分.我也有从Task派生的BackgroundTask和从BackgroundTask派生的UserAccountTask.

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

当我尝试创建类型为Task的对象并将其添加到任务存储库中时,只要将更改保存到数据库中,我就会得到一个DbUpdateException.其内部异常状态:

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:

无效的列名称'UserAccount_UserId'.\ r \ n无效的列名称'UserAccount_Lastname'.\ r \ n无效的列名称'UserAccount_Firstname'.\ r \ n无效的列名称'UserAccount_Fullname'. .\ r \ n无效的列名'UserAccount_Title'[...]"

"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是另一种类型,并且是UserAccountTask的属性(UserIdLastname等是UserAccount的属性).

UserAccount is another type and a property of UserAccountTask (UserId, Lastname etc. are properties of UserAccount).

我希望我对问题的描述不会太混乱:-/ 我只是想让EF忽略Task是其他某种类型的基类的事实,因为恕我直言,那时它并不重要.

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.

谢谢你,凯文

推荐答案

尝试在派生上下文中使用它:

Try to use this in your derived context:

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

这篇关于让实体框架忽略派生类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆