.net 实体框架中的计算属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1131908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculated properties in Entity Framework
提问by Chris B. Behrens
Suppose I have an Employee object with the following properties:
假设我有一个具有以下属性的 Employee 对象:
string Name { get; }
float Hours { get; }
float Wage { get; }
I want to add a property, Salary, which equals Hours * Wage. In an ordinary business object, I would simply code that up in the property, but this would presumably wiped out if the class needed to be regenerated.
我想添加一个属性,薪水,它等于小时 * 工资。在一个普通的业务对象中,我会简单地在属性中对其进行编码,但是如果需要重新生成类,这可能会消失。
Is there an EF standard way to implement this without going through the trouble of mapping it to a database entity?
是否有 EF 标准方法来实现这一点,而无需麻烦地将其映射到数据库实体?
回答by JoshJordan
Indeed. Create a separate file, for instance, EmployeeExtension.cs.
的确。创建一个单独的文件,例如EmployeeExtension.cs。
In this file, place the following code:
在此文件中,放置以下代码:
public partial class Employee
{
public decimal Salary
{
get { return Hours * Wage; }
}
}
LINQ to SQL and Entity Framework classes are generated with the partialkeyword to allow you to split the definition over many files, because the designers knew that you will want to add members to the class that aren't overwritten by continually autogenerating the base source file.
LINQ to SQL 和实体框架类是使用partial关键字生成的,以允许您将定义拆分到多个文件中,因为设计人员知道您将希望向类添加不会通过不断自动生成基本源文件而覆盖的成员.
回答by Argo
If i remember correctly the classes created by the EF are partial. So you could possibly add another file containing another partial class (same namespace, same classname of course) which than implements the property
如果我没记错的话,EF 创建的类是部分类。所以你可以添加另一个包含另一个部分类(相同的命名空间,当然是相同的类名)的文件,它实现了该属性
public single Salary
{
get
{
return this.Hours * this.Wage;
}
}
Should do the trick (if those singles are not nullable, mind you!)
应该可以解决问题(如果这些单曲不可为空,请注意!)
回答by Philip.ie
I was not able to get 'Argo's answer to work initially. After a little playing around I noticed that if i decorated the property (as in WCF), which the following attribute, all worked fine.
我最初无法得到 'Argo 的回答。在玩了一会儿之后,我注意到如果我装饰了以下属性的属性(如在 WCF 中),则一切正常。
[global::System.Runtime.Serialization.DataMemberAttribute()]
As per 'Argo's instructions create a separate file, (for example EmployeeExtension.cs). this should be marked partial as described.
根据“Argo 的说明”创建一个单独的文件(例如 EmployeeExtension.cs)。这应该按照描述标记为部分。
In this file, place the following code:
在此文件中,放置以下代码:
public partial class Employee
{
[global::System.Runtime.Serialization.DataMemberAttribute()]
public decimal Salary
{
get { return Hours*Wage; }
}
}
Hope this helps….
希望这可以帮助…。
回答by Martin Liversage
You can implement the property in the entity class. The entity framework will generate partial classes allowing you to add members to the class. Add a class like this to your code:
您可以在实体类中实现该属性。实体框架将生成部分类,允许您向类添加成员。将这样的类添加到您的代码中:
public partial class Employee {
public Single Salary {
get { return Hours*Wage; }
}
}
回答by Elijah W. Gagne
This wasn't working for me with Entity Framework 5. The solution for me was to simply use the [NotMapped] attribute. For more info see Code First DataAnnotations
这对 Entity Framework 5 不起作用。我的解决方案是简单地使用 [NotMapped] 属性。有关更多信息,请参阅Code First DataAnnotations

