EF代码优先 - 如何在表层次映射上防止创建表上的重复列 [英] EF Code First - How to prevent duplicate column on created table on Table-Per-Hierarchy Mapping

查看:857
本文介绍了EF代码优先 - 如何在表层次映射上防止创建表上的重复列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的实体

public abstract class MyBaseClass
{
    public int Id { get; set; }
}

public class MyConcrete : MyBaseClass
{
    public int TemplateName { get; set; }
    public int Total { get; set; }
}

public class MyOtherConcrete : MyBaseClass
{
    public int TemplateName { get; set; }
    public int MyProperty { get; set; }
    public string OtherProperty { get; set; }
}

使用默认初始化,EF将使用像下面这样的列: p>

using default initialization, EF will make table with columns like bellow:

Id
TemplateName
TemplateName1 // <-- this is the problem
Total
MyPorperty
MyOtherProperty

现在我的问题如何配置EF,所以所有的TemplateName属性派生类自动映射到TemplateName列而不进行另一列。可以在 OnModelCreating 方法上进行配置?

now my question how to configure EF so all the TemplateName property on derived class automatically mapped into TemplateName column without making another column. is it possible to configure it on OnModelCreating method?

编辑

实际上面是我的问题的简化版本。我有10个更多的实体,一些属性可能在任何地方重复,我不想添加任何抽象层。

actually above was simplified version of my problem. i have 10 more entities some property might duplicated everywhere and i don't want to add any abstraction layers.

我已经尝试手动映射列在 OnModelCreating 但具有类型中的每个属性名称必须是唯一的。属性名称TemplateName已被定义异常任何想法?

i have tried manually map the column on the OnModelCreating but having "Each property name in a type must be unique. Property name 'TemplateName' was already defined" exception any idea?

编辑2

所以,我发现这里,那说的是不可能做这样的事情像上面的EF,对我来说很奇怪..

so, i found here, that said it is impossible to do such thing like above in EF, it is weird for me..

推荐答案

搜索网后,到目前为止,这是不可能的。因为我意识到我的继承树是错误的。

After searching through the net, so far that was not possible to do that. since i realize that my inheritance tree is wrong.

所以在我的情况下,我应该改变我的代码以匹配EF要求,这听起来很奇怪..因为在很多情况下,ENTITY是一个没有改变,我们通常创建一个在多个项目中使用的实体。事件我们发现我们的实体错误,我们不会更新它,因为更新将需要对其他项目进行大量更改。

so in my case, i should change my code to match the EF requirement, it is sound weird.. because in many case ENTITY is a NO NO to change, we usually create an entity that used in multiple project. event we found our entity wrong we won't update it because updating it will require massive change on the other projects.

到目前为止,我认为没有确切的答案我的问题。将在EF支持后立即更新。

so far, i think there is no exact answer for my question. will update it soon after EF support it.

这篇关于EF代码优先 - 如何在表层次映射上防止创建表上的重复列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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