开发者

wcf data service with Entity Framework

I am trying to create WCF Data service project using Entity Framework. ( I am new to both).

I created entities using DB. 开发者_StackOverflow中文版Now, I created service operation, which returns the IQueryable<entity>.

My problem is

  1. I do not want to return the entire set of columns in the entity. I cannot delete them from the entity as it is not null. How to avoid these?

  2. I have few FK columns and I need other column details of the table. How to include columns from other table?

  3. Why it is not possible to use POCO class to be returned from WCF Data service?

  4. How do I format the response; i.e., add few more details to the response like page number etc, change the xml tags, remove few details like "link rel"?

I have tried a lot of things to achieve 1 and 2. But finally I realised that I can only use the entity as it is to make it work.

I have no idea about 4.

Any suggestions would be appreciated.


1. I do not want to return the entire set of columns in the entity.
2. I have few FK columns and I need other column details of the table.

For this, you should define a new class that matches what you need / what you want your clients to see. That can be a straight POCO class - no special requirements. Assemble that class for each entity, leaving out the unwanted columns, and grabbing the extra field or two for the FK columns into that new class. Return an IQueryable<YourNewClass> instead of the entity class directly.

To avoid huge left-right-assignment statements just to fill the properties of the new class, you should have a look at AutoMapper which makes it really easy to copy around classes that are very similar to one another (e.g. missing or adding a few properties).

4. How do I format the response; i.e., add few more details to the response like page number etc, change the xml tags, remove few details like "link rel"?

That's not possible - the OData protocol very strictly defines what's going to be in the message, what links there are etc. If you can't live with that - you'll have to roll your own WCF REST service and drop the WCF Data Service stuff altogether.

Check out the WCF REST Developer Center on MSDN if you want to investigate that route more thoroughly.

Update: that link seems to have gone dead - try WCF Web Http Programming Overview instead.


Make sure you have an Id property or you specify either [Key] or [DataServiceKey("Your_Custom_ID_Property")] For me this sorted out the issue

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜