I have an entity model which has some tables added from my DB. I want to include a custom class which will act as a data model and will return customized data. Here is what I am trying to do:
// 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;
}
}
However I want to include this Employees class in my existing entity data model class, so that rather than creating separate service with different data model, I extend my existing data model to include my custom class (EmployeesModel) too.
Why would you want to expose a customized view over an existing entity? The client can do this anyways and customize them in the way they want using projections. The query on the client side would exactly look like the one who wrote above to initialize the Employees property.
Currently, there is no way to do this. We are looking into ways to make sure happen in our next release. No promises though, but there is one of the things we have been asked a number of times and its pretty high in our proirity list.
Use this voting site to vote for this feature: http://blogs.msdn.com/b/astoriateam/archive/2010/09/10/what-do-you-want-to-see-added-changed-in-wcf-data-services.aspx
Thanks
Pratik