问题描述
我正在使用通过EF4.3使用单表继承的一些代码。
有一个名为User的实体,称为Admin的另一个实体。管理员继承自用户。
用户类
public class User
{
public int Id {get; set;}
public string Username {get; set;}
}
管理类
public class Admin:User {}
EF生成了一个判别器
列。当添加 Admin
记录时, Discriminator =Admin
,用户记录将具有 Discriminator =User
。
我的问题是,如何更新 Discriminator
列,如果我想要一个用户
进入和管理
?
我尝试从一个对象类型转换到另一个对象,并使用EF保存。但是不会更改Discriminator列。感谢您的帮助。
对象永远不能改变他们的类型。如果您打算为用户(合理)更改管理员设置,则应该使用属性/属性而不是类型名称。
例如,您可以在用户
对象和管理员
对象之间建立关联,而不是希望替换 用户
对象与管理员
子类型。
:
要直接回答您的问题,无法使用EF更改此列。你当然可以直接在SQL中执行。但是由于上述原因,我鼓励您改变这种设计。
I am working on some code that makes use of single table inheritance via EF4.3.
There is an entity called User and another entity called Admin. Admin inherits from user.
User class
public class User
{
public int Id {get;set;}
public string Username {get;set;}
}
Admin class
public class Admin : User { }
EF generated the database tables with a Discriminator
column. When an Admin
record is added then Discriminator="Admin"
and a User record would have Discriminator="User"
.
My question is, how do I update the Discriminator
column if I want to make a User
into and Admin
?
I tried casting from one object type to the other and saving using EF. but that doesn't change the Discriminator column. How do I change it?
Thanks for any help.
Objects can't change their types, ever. If you intend to change the "administrator" setting for a user (reasonable), you should do that with an attribute/property, not a type name.
For example, you could have an association between a User
object and an Administrator
object, rather than wanting to replace the User
object with an Administrator
subtype.
I wrote about this idea in an old blog post:
To answer your question directly, there's no way to change this column using EF. You could of course, do it in straight SQL. But for the reason given above, I'd encourage you to change this design.
这篇关于如何更改自动生成的“鉴别器”列EntityFramework?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!