与表每类型的继承实体框架模型 [英] Entity Framework Model with Table-per-Type Inheritance

查看:187
本文介绍了与表每类型的继承实体框架模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我定义的实体框架模型与表每类型的继承,如果我有一个基类/表(不是抽象的)叫的人,和两个子实体和表格,成人和儿童,创建子后,我将如何采取同样的对象,并将其转换为成人?一旦转化为成人,儿童记录应被删除,但在人员表的基类的数据应予以保留。

When I define a model in Entity Framework with Table-per-Type Inheritance, if I have a base class/table (not abstract) called person, and two sub entities and tables, adult and child, after creating a child, how would I take the same object and convert it to adult? Once converted to adult, the child record should be deleted, though the base class data in the person table should be retained.

推荐答案

这是不可能的。这是类似的问题,如这里。只要实体存在,它的类型是不可变的。要做到这一点的唯一方法是删除子实体(两个表=记录),并创建一个新的成人实体(=新的记录两个表)。

It is not possible. It is similar problem like here. Simply the entity exists and its type is immutable. The only way to do that is delete child entity (= records from both tables) and create a new adult entity (= new records for both tables).

这看起来并不像场景继承的。

This doesn't look like scenario for inheritance at all.

编辑:

关于继承的注释是针对在那里你提到的情况下成人儿童实体。无论如何,一旦你的情况下允许改变,你应该考虑其他的解决方案,它可以改变部分将通过组合处理的类型。

The comment about inheritance was targeted for scenario where you mentioned Person, Adult and Child entities. Anyway once your scenario allows changing the type you should think about another solution where the part which can change will be handled by composition.

例如:

public class DataSource
{
    public int Id { get; set; }
    public virtual DataSourceFeatures Features { get; set; }
}

public class DataSourceFeatures
{
    [Key, ForeignKey("DataSource")]
    public int Id { get; set; }
    public virtual DataSource DataSource { get; set; }
}

public class XmlDataSourceFeatures : DataSourceFeatures { ... }

public class DelimitedDataSourceFeatures : DataSourceFeatures { ... }

public class ServiceDataSourceFeatures : DataSourceFeatures { ... }

现在改变一种手段删除相关的电流 DataSourceFeatures 从数据库中,并创建一个新的,但原始对象仍然是相同的 - 只有关系的变化

Now changing a type means deleting dependent current DataSourceFeatures from the database and create a new one but original object remains the same - only relation changes.

这篇关于与表每类型的继承实体框架模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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