How to create a computed property in Data Services (OData)?

微笑、不失礼 提交于 2019-11-27 18:28:06

问题


I am creating an OData service with WCF Data Services using an EDMX. How can I create a computed property for an entity type, so that its value gets computed in the service (C#) and does not come from the database?
The value of this property is based on the value of other properties, which are mapped to fields in the database.


回答1:


If you are exposing your EDMX file directly, using the default Entity Framework Provider for Data Services, something like this:

public class MyService: DataService<MyEntities> {

Then unfortunately you can't expose any 'new' properties that aren't in the underlying Entity Framework EDM model.

Having said that you have other options, you could write a reflection provider or custom provider that adds the extra property and delegates most of the work to EF under the hood.

The problem is setting up all the delegation is NOT easy today.

This series of posts explains providers and shows how to create a custom provider based service, and this one shows how to create a service using the Reflection Provider.




回答2:


The solution I found is to use Entity Framework Code First instead of an EDMX. It allows you to create computed properties just by creating standard properties in code.
Here is an example:

public class Person
{
  public String FirstName { get; set; }
  public String LastName { get; set; }
  public String FullName
  {
    get { return FirstName + " " + LastName; }
  }
}


来源:https://stackoverflow.com/questions/3863113/how-to-create-a-computed-property-in-data-services-odata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!