使用实体框架的 wcf 数据服务
我正在尝试使用实体框架创建 WCF 数据服务项目。 (我对两者都是新手)。
我使用数据库创建了实体。 现在,我创建了服务操作,它返回 IQueryable
。
我的问题是
我不想返回实体中的整个列集。我无法从实体中删除它们,因为它不为空。如何避免这些?
我有几个 FK 列,我需要表的其他列详细信息。如何包含其他表中的列?
为什么无法使用从 WCF 数据服务返回的 POCO 类?
如何格式化响应;即,向响应添加更多详细信息(例如页码等)、更改 xml 标记、删除一些详细信息(例如“link rel”)?
我尝试了很多方法来实现1和2。但最终我意识到我只能按原样使用实体才能使其发挥作用。
我不知道 4。
任何建议将不胜感激。
I am trying to create WCF Data service project using Entity Framework. ( I am new to both).
I created entities using DB.
Now, I created service operation, which returns the IQueryable<entity>
.
My problem is
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?
I have few FK columns and I need other column details of the table. How to include columns from other table?
Why it is not possible to use POCO class to be returned from WCF Data service?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为此,您应该定义一个与您需要的/您希望客户看到的内容相匹配的新类 这可以是一个直接的 POCO 类 - 没有特殊要求。为每个实体组装该类,忽略不需要的列,并将 FK 列的额外字段或两个字段放入该新类中。返回一个
IQueryable
而不是直接返回实体类。为了避免仅仅为了填充新类的属性而使用大量的左右赋值语句,您应该查看 AutoMapper 这使得在彼此非常相似的类之间进行复制变得非常容易(例如缺少或添加一些属性)。
这是不可能的 - OData 协议非常严格地定义了消息中的内容、有哪些链接等。如果您不能忍受这一点 - 您将不得不滚动您的拥有 WCF REST 服务并完全放弃 WCF 数据服务内容。
如果您想更彻底地研究该路线,请查看 MSDN 上的 WCF REST 开发人员中心。
更新:该链接似乎已失效 - 尝试 WCF Web Http 编程概述。
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).
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.
确保您有
Id
属性,或者指定[Key]
或[DataServiceKey("Your_Custom_ID_Property")]
对我来说这解决了问题
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