0
我有一个实体模型,其中有一些表从我的数据库中添加。我想包含一个自定义类,它将充当数据模型并将返回自定义数据。这就是我要做的:扩展默认实体框架模型类以包含自定义类
// 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)。
原因在我的默认实体暴露定制的看法是,我试图从连接的更多获取定制数据比1表(上面的代码只是一个示例,所以我用一个表来返回自定义字段),然后使用分页。当然,我可以通过揭露服务操作来做到这一点,但是我没有得到总记录数,正如pablo castro在他的一篇文章中所说的,需要解析整个表达式树并且可能不可行。如果这是不可能的,我可能需要公开自定义数据模型并构建单独的数据服务,并且可维护性将很困难。 – Ashish 2011-03-15 03:22:02