EntityFramework如何覆盖属性 [英] EntityFramework how to Override properties

查看:137
本文介绍了EntityFramework如何覆盖属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始在VS2010中使用EF。那个东西只是amazin'。



我坦白说不明白什么。例如我有EntityType属性,它们是从数据库结构生成的。
现在,我必须在代码中重写该属性。我不需要将属性的值保存回DB中,但是每次从DB读取数据时,都应该用运行时计算值代替。



当然,我可以根据我的EntityType创建派生类,但我尝试了一些困难,我不知道这样做是正确的。无论如何,即使我尝试将整个EntityType更改为抽象,那么可怕的Visual Studio也不想验证它,并且说明如下:



错误2078:EntityType AssetsModel.Asset'是摘要,只能使用IsTypeOf映射。
错误2063:在Assets的集合映射中至少必须映射一个属性。



这是什么意思,这意味着我不知道。 。



任何想法?

解决方案

最好的方法是使用Partial Classes,然后创建一个新的ReadOnly属性来计算getter中的值。 / p>

I've just started using EF in VS2010. That thing is just amazin'.

I frankly can't understand something. For example I have EntityType with property, they generated from database structure. Now, I have to simply override that property in my code. I don't need to save value of the property back into DB, but everytime when it gets read from DB it should be substituted with run-time calculated value.

Of course I can create derived class based on my EntityType but I've tried and found kinda difficulties, I'm not sure this is kinda right way to do. Anyway even when I try to change the whole EntityType to Abstract, damn Visual Studio doesn't want to validate that and says something like:

"Error 2078: The EntityType 'AssetsModel.Asset' is Abstract and can be mapped only using IsTypeOf." "Error 2063: At least one property must be mapped in the set mapping for 'Assets'"

What the hell is this suppose to mean I dunno..

Any ideas?

解决方案

The best approach would be to use Partial Classes and then create a new ReadOnly property to calculate the value in the getter.

这篇关于EntityFramework如何覆盖属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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