更改作为继承层次结构一部分的(实体框架)实体的类型 [英] Changing the type of an (Entity Framework) entity that is part of an inheritance hierarchy

查看:20
本文介绍了更改作为继承层次结构一部分的(实体框架)实体的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个继承层次结构,其中包含一个基本员工实体和一些特定员工类型的后代实体.我需要能够将基本员工实体转换为更具体的实体(例如 TemporaryEmployee),并从更具体的类型转换回基本类型(例如,如果员工不再是临时员工",那么我希望该实例只是作为员工坚持.在数据库中,这只是为特定子类从表中添加或删除行的问题.(我在每个班级都使用表.)不过,我没有看到如何使用 EF 调用来做到这一点.

I have an inheritance hierarchy with a base Employee entity and some descendent entities for specific employee types. I need to be able to convert a base Employee entity to a more specific entity (e.g. TemporaryEmployee) and from a more specific type back to the base type (e.g. if an employee is no longer "temporary" then I want that instance to just be persisted as Employee. In the DB this is just a matter of adding or removing a row from the table for the specific subclass. (I'm using table per class.) I'm not seeing how to do this using EF calls though.

推荐答案

从技术上讲,可以通过使用存储过程来实现.TPT 不支持.

Technically, you can achieve it by using the stored procedure. TPT does not support it.

不过,我完全同意克雷格的观点.在经典的编程书籍 Design Patterns (Addison-Wesley Professional) 中,作者讨论了继承与组合的问题,并得出结论,人们应该更喜欢组合而不是继承".

However, I totally agree with Craig. In classic programming book Design Patterns (Addison-Wesley Professional), authors discuss inheritance versus composition and conclude that one should "favor composition over inheritance."

这篇关于更改作为继承层次结构一部分的(实体框架)实体的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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