如何在Code First Entity Framework中避免使用“ discriminator”列 [英] How can 'discriminator' column be avoidable in Code First Entity Framework

查看:85
本文介绍了如何在Code First Entity Framework中避免使用“ discriminator”列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我从模型类继承我的业务类(模型类映射在数据库表上),那么如何避免在数据库内部创建一个列作为 Discriminator。



<因为目前,如果我将我的业务类(例如,Specifics)继承到现有的模型类(例如,DataSpecific),它会要求代码优先迁移。在迁移中,我可以将鉴别器视为新列。我真的不想要这个因为在整个应用程序中都使用了原始模型类,所以该代码可以正常工作。



如何停止创建描述符列



C#代码:



模型类

 公共类DataSpecific 
{

}

商务舱

 公共舱特定
{

}

结果,我可以在迁移中看到以下代码

  AddColumn( dbo.Consignments, Discriminator,c => c.String(nullable:false,maxLength:128)); 

如何避免这种情况?

解决方案

要么应用 NotMapped 对您的业务类别的数据注释:

  [NotMapped] 
public class Specific:数据特定
{
}

或使用 忽略 Fluent API:

  modelBuilder.Ignore< Specific>(); 


How can I avoid creating a column as "Discriminator" inside the database , If I inherit my business class from model class ( model class is mapped on database table).

Because, at the moment, if I inherit my business class ( e.g Specifics) to an existing model class ( i.e DataSpecific ), It asks for code first migration. In the migration, I can see discriminator as new column. I really don't want this. Because, original model class is being used in the whole application and that code works fine.

How can I stop the creation of "descriminator" column

C# Code :

Model Class

public class DataSpecific
{

}

Busines Class

public class Specific
{

}

as a result I can see following code in the migration

 AddColumn("dbo.Consignments", "Discriminator", c => c.String(nullable: false, maxLength: 128));

How can I avoid this?

解决方案

Either apply NotMapped Data Annotation to your business class:

[NotMapped]
public class Specific : DataSpecific
{
}

or use Ignore Fluent API:

modelBuilder.Ignore<Specific>();

这篇关于如何在Code First Entity Framework中避免使用“ discriminator”列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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