实体框架:继承,更改对象类型 [英] Entity Framework: Inheritance, change object type

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

问题描述

假设我在模型中有2个类:User(映射到USERS表)和PrivilegedUser(继承User,其他信息存储在PRIVILEGEDUSERS表中)。

Let's say, I have 2 classes in the model: User (mapped to USERS table) and PrivilegedUser (inherits User, additional info is stored in PRIVILEGEDUSERS table).

现在,我在USERS(和User的实例)中有一行,需要将该用户转换为PrivilegedUser(即在PRIVILEGEDUSERS中创建具有相同Id的记录)。有没有办法在没有删除/插入的情况下执行此操作?

Now, I have a row in USERS (and instance of User) and need to convert that user to PrivilegedUser (i.e. to create a record in PRIVILEGEDUSERS with the same Id). Is there a way to do this without Delete/Insert?

问题是您在模型中没有PRIVILEGEDUSERS表示,因此您不能仅创建PrivilegedUser的那部分。

The problem is you don't have PRIVILEGEDUSERS representation in the model, so you cannot create only that part of PrivilegedUser.

这只是一个例子。 PrivilegedUser可能有一些折扣或个人经理或其他 <普通用户属性。同时,无论具体的用户类型如何,还有其他表需要引用用户。我已经使用Table-per-Type继承模式实现了它。在数据库级别,将用户从一种类型转换为另一种类型非常简单(您只需要从扩展表中插入或删除记录)。但在EF中,您只有UserSet,它存储User和PrivilegedUser对象。这就是为什么我要问有可能用PrivilegedUser替换现有的用户对象,保留现有的Id而不删除USERS表中的记录。

It was just an example. PrivilegedUser may have some discount or personal manager or whatever in addition to ordinary User properties. In the same time, there are other tables which need to reference users regardless of concrete User type. I've implemented it using Table-per-Type inheritance mode. In the database level it's very simple to convert users from one type to another (you just need to insert or delete record from extension table). But in EF you have only UserSet which stores both User and PrivilegedUser objects. That's why I ask is it possible to replace existing User object with PrivilegedUser keeping existing Id and without deleting record from USERS table.

推荐答案

不,你不能。

正如本文,EF(3.5)不支持此功能。您必须使用存储过程来完成此操作。

As explained by this article, EF (3.5) does not support this feature. You must use stored procedure to accomplish this.

这篇关于实体框架:继承,更改对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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