如何更改自动生成的“鉴别器”列EntityFramework? [英] How to change the auto-generated "Discriminator" column in EntityFramework?

查看:138
本文介绍了如何更改自动生成的“鉴别器”列EntityFramework?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用通过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列。感谢您的帮助。

解决方案

对象永远不能改变他们的类型。如果您打算为用户(合理)更改管理员设置,则应该使用属性/属性而不是类型名称。



例如,您可以在用户对象和管理员对象之间建立关联,而不是希望替换 用户对象与管理员子类型。



我在旧博客文章中写过这个想法


在设计好的对象关系映射时必须克服的一个心理障碍是主要以面向对象术语的倾向,或关系条款,无论适合您的个性。然而,良好的对象关系映射既包含了良好的对象模型和良好的关系模型。例如,假设您有一个包含People的表的数据库,以及Employees和Customers的相关表。一个人可能在所有三个表中都有一个记录。现在,从严格的关系来看,您可以为员工构建数据库VIEW,为客户构建另一个数据库视图,两者都包含People表中的信息。当使用一个VIEW或其他视图时,您可以将个人暂时认为是只是员工或只是一个客户,即使您知道他们都是。所以来自这个世界观的人可能会试图做一个OO映射,其中Employee和Customer都是Person的(直接)子类。但这并不适用于我们所拥有的数据;由于单个人具有员工和客户记录(并且由于任何人员实例可以同时具有具体子类型Employee和Customer),因此Person和Employee之间的OO关系需要组合而不是继承,对于Person和Customer也是类似的。


要直接回答您的问题,无法使用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:

One of the mental barriers that you have to get over when designing a good object relational mapping is the tendency to think primarily in object oriented terms, or relational terms, whichever suits your personality. A good object relational mapping, though, incorporates both a good object model and a good relational model. For example, let’s say you have a database with a table for People, and related tables for Employees and Customers. A single person might have a record in all three tables. Now, from a strictly relational point of view, you could construct a database VIEW for employees and another one for customers, both of which incorporate information from the People table. When using a one VIEW or the other, you can temporarily think of an individual person as "just" an Employee or "just" a Customer, even though you know that they are both. So someone coming from this worldview might be tempted to do an OO mapping where Employee and Customer are both (direct) subclasses of Person. But this doesn’t work with the data we have; since a single person has both employee and customer records (and since no Person instance can be of the concrete subtype Employee and Customer simultaneously), the OO relationship between Person and Employee needs to be composition rather than inheritance, and similarly for Person and Customer.

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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