【发布时间】:2011-03-14 09:27:50
【问题描述】:
我有一个实体模型,其中有一些从我的数据库中添加的表。我想包含一个自定义类,它将充当数据模型并返回自定义数据。这是我想要做的:
// My custom data model
public class DataModel
{
var dbContext = new ODataDemoEntities();
Employees = from e in dbContext.Employee
select new EmployeeModel
{
ID = e.EmployeeID,
FirstName = e.FirstName,
LastName = e.LastName
};
public IQueryable<EmployeeModel> Employees { get; private set; }
}
// My custom class
[DataServiceKey("ID")]
public class EmployeeModel
{
/// <summary>ID of the employee.</summary>
public int ID { get; set; }
/// <summary>First name of the employee.</summary>
public string FirstName { get; set; }
/// <summary>Last name of the employee.</summary>
public string LastName { get; set; }
}
// My WCF Data Service Code
public class EmployeeDataService : DataService<DataModel>
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("Employees", EntitySetRights.AllRead);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
}
但是,我想在我现有的实体数据模型类中包含这个Employees 类,这样我就不会使用不同的数据模型创建单独的服务,而是扩展我现有的数据模型以包含我的自定义类(EmployeesModel)。
【问题讨论】:
标签: entity-framework-4 wcf-data-services