扩展实体框架模式,包括新的属性 [英] Extending Entity Framework Model to include new property

查看:141
本文介绍了扩展实体框架模式,包括新的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的EF所以请原谅我,如果这是一个小白的问题。

I'm new to EF so please excuse me if this is a noob question.

基本上,我们有一个EF模型设置使用模式首先为我们的平台项目,并在许多应用中,我们建立在这个平台之上是共享的。在这些应用中,我们希望扩展类,而无需改变模型的平台,包括额外的属性。这可能与EF 4,如何将我能够做到这一点,而无需修改.edmx文件?

Basically, we have a EF model set up using Model First for our 'platform' project and is shared across many applications which we build on top of this platform. In some of these applications we want to extend the classes to include additional properties without changing the model in the platform. Is this possible with EF 4 and how would I be able to do it without modifying the .edmx file?

我注意到,生成的类都偏所以可能我可以创建具有相同名称的新部分类,包括新的属性,但有没有需要被照顾的任何映射?

I notice that the generated classes are all partial so potentially I could create a new partial class with the same name to include the new properties but is there any mappings that need to be taken care of?

P.S。一般情况下我不得不pferred使用继承并创建一个新类来保存新的属性,而不是却又$ P $,我不知道该怎么做,与EF ..这里有什么启示将大大AP preciated!

p.s. under normal circumstances I'd have preferred to use inheritance and create a new class to hold the new properties instead but again, I don't know how to do that with EF.. any enlightenment here will be much appreciated!

非常感谢,

推荐答案

您无法使用继承,因为一旦实体从数据源加载EF不会知道继承和发展,因为它会实例化基本类型没有你的属性,而不是派生类型与你的属性。任何继承必须EDMX映射如果EF有它的工作。

You cannot use inheritance because once entity is loaded from the data source EF will not know about inheritance and because of that it will instantiate base type without your properties instead of derived type with your properties. Any inheritance must be mapped in EDMX if EF have to work with it.

使用部分类将解决您的问题,但:

Using partial class will solve your problem but:

  • 部分类的所有部分必须在同一个组件定义
  • 从您的局部部分属性不会持久化到数据库
  • 从您的局部部分属性不能在LINQ到实体查询中使用

这篇关于扩展实体框架模式,包括新的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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