问题描述
假设我有具有以下属性Employee对象:
Suppose I have an Employee object with the following properties:
string Name { get; }
float Hours { get; }
float Wage { get; }
我想补充一个属性,工资,相当于小时*工资。在一个普通的业务对象,我只是$ C c表示$在属性,但这将presumably全军覆没,如果需要的类再生。
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.
有没有实现这一点没有通过映射它的麻烦去一个数据库实体的EF标准的方式?
Is there an EF standard way to implement this without going through the trouble of mapping it to a database entity?
推荐答案
确实。创建一个单独的文件,举例来说, EmployeeExtension.cs 的
Indeed. Create a separate file, for instance, EmployeeExtension.cs.
在这个文件中,把下面的code:
In this file, place the following code:
public partial class Employee
{
public decimal Salary
{
get { return Hours * Wage; }
}
}
LINQ to SQL和实体框架类的部分的关键字,让您可以拆分成多个文件的定义产生的,因为设计师知道你将要成员添加到AREN类T覆盖通过不断autogenerating基源文件。
LINQ to SQL and Entity Framework classes are generated with the partial keyword 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.
这篇关于在实体框架计算性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!