在C#中由实体框架生成的类派生 [英] Deriving from classes generated by Entity Framework in C#

查看:168
本文介绍了在C#中由实体框架生成的类派生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个实体数据模型,并产生从它的数据库中。

I have created an entity data model and generated a database from it.

一个实体被称为模板

产生的部分类来扩展模板的功能正常工作。

Created partial classes to extend the functionality of Template works fine.

如果我创建一个新类,并尝试从模板来推导,我在实例化得到一个运行时异常:

If I create a new class and try to derive from Template, I get a runtime exception upon instantiating:

映射和元数​​据信息不能为的EntityType'Template001找到。

如何我可以解决此问题?我肯定需要从EF类继承。

How can I work around this? I definitely need to inherit from the EF classes.

修改

不似乎可能。如果是这样的话,这将是实现以下要求的最佳方式:模板实体存储有关模板,每个人都有自己的代码来执行的信息。这就是为什么我试图从摆在首位实体派生。

Does not seem possible. If that is the case, what would be the best way to implement the following requirement: The template entity stores information about templates that each have their own code to execute. That is why I was trying to derive from the entity in the first place.

推荐答案

为什么你需要从实体类继承首先?如果你想添加一些简单的行为,使用部分类。

Why do you need to inherit from entity class first of all? If you want to add some simple behavior, use partial class.

更新:根据意见,似乎有可能性的行为会随着时间延长。在这种情况下,我会建议使用成分/汇聚,而不是继承。让需要被扩展的类有一个实体的字段。在Raheel的情况下,这将是一个名为TemplateLogic与模板类型字段/属性类。

Update: Based on comments, it appears that there is possibility that behavior will be extended over the time. In this case, I would recommend using composition/aggregation, not inheritance. Let the classes that need to be extended have an entity as a field. In Raheel's scenario, it would be a class called TemplateLogic with field/property of type Template.

这篇关于在C#中由实体框架生成的类派生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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