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

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

问题描述

我在一个带有 MySQL 的网站上使用 EF CF 方法.出于某种原因,EF 在我的 Post 表中创建了一个名为Discriminator"的列,并包含 VARCHARPost".

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?

推荐答案

Discriminator 列在 Table-Per-Hierarchy 继承场景.例如,如果您有这样的模型......

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
{
    //...
}

...并使 BaseEntity 成为模型的一部分,例如通过将 DbSet 添加到您的派生上下文中,Entity Framework 将映射此类层次结构默认情况下放入单个表中,但引入一个特殊列 - Discriminator - 以区分存储在其中的不同类型(PostOtherEntity)这个表.此列填充了类型的名称(同样是 PostOtherEntity).

... 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天全站免登陆