实体框架代码首先创建“鉴别器”柱 [英] Entity framework code first creates "discriminator" column

查看:93
本文介绍了实体框架代码首先创建“鉴别器”柱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EF CF方法与MySQL的网站。
由于某种原因,EF在我的Post表中创建一个名为Discriminator的列,并包含VARCHARPost。



为什么这个列被创建?我可以做一些事情来避免它被创建吗?

解决方案

c c>在 表层次结构 继承方案。如果你有这样的模型...

  public abstract class BaseEntity 
{
public int Id {get;组; }
// ...
}

public class发布:BaseEntity
{
// ...
}

public class OtherEntity:BaseEntity
{
// ...
}

...并使模型的 BaseEntity 部分,例如添加一个 DbSet< BaseEntity> 到您的派生上下文中,Entity Framework将默认将该类层次结构映射到单个表中,但引入一个特殊列 - Discriminator 来区分存储在此表中的不同类型( Post OtherEntity )。此列填充了类型的名称(再次 Post OtherEntity )。


I am using EF CF approach for a website with MySQL. For some reason EF creates a column in my Post table called "Discriminator" and contains the VARCHAR "Post".

Why is this column created? Can I do something to avoid it being created? Are there any advantages of having this column?

解决方案

The Discriminator column is used and required in Table-Per-Hierarchy inheritance scenarios. If you for example have a model like this ...

public abstract class BaseEntity
{
    public int Id { get; set; }
    //...
}

public class Post : BaseEntity
{
    //...
}

public class OtherEntity : BaseEntity
{
    //...
}

... and make the BaseEntity part of the model, for instance by adding a DbSet<BaseEntity> to your derived context, Entity Framework will map this class hierarchy by default into a single table, but introduce a special column - the Discriminator - to distinguish between the different types (Post or OtherEntity) stored in this table. This column gets populated with the name of the type (again Post or OtherEntity).

这篇关于实体框架代码首先创建“鉴别器”柱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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