WCF,实体框架&数据合同 [英] WCF, Entity Framework & Data Contracts

查看:91
本文介绍了WCF,实体框架&数据合同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用VS 2008& .NET 3.5 SP1:

Using VS 2008 & .NET 3.5 SP1:

我使用WCF允许客户端连接到使用Entity Framework读取和写入数据库条目的服务。默认情况下,从数据库自动生成的实体具有应用的DataContract属性。

I am using WCF to allow clients to connect to a service that reads and writes database entries using Entity Framework. By default the entities that are generated automatically from the database have the DataContract attribute applied.

不幸的是,许多字段都不是客户端消费的(即 - 记录谁正在访问什么数据等),出于安全考虑,我宁愿让他们不被暴露。有没有办法避免实体框架类以这种方式暴露?

Unfortunately many of the fields are exposed are not meant for consumption by the client (i.e. - records of who is accessing what data, etc.) and for security reasons I would rather keep them from being exposed. Is there any way to avoid Entity Framework classes from being exposed in this manner?

注意 :这不是如何复制防止.NET实体中的私有属性通过服务暴露为public?在这个问题上,用户希望有选择地显示某些字段,而我希望该实体根本不被公开为DataContract。

Note: This is not a duplicate of How to prevent private properties in .NET entities from being exposed as public via services?. In that question the user wishes to selectively display certain fields, whereas I would like the entity to not be exposed as a DataContract at all.

提前感谢。 >

Thanks in advance.

推荐答案

您是否知道您的实体不需要与数据库映射一对一?特别是,您可以省略列,甚至是不相关的整个表。

Are you aware that your entities do not need to map one to one with the database? In particular, you can leave out columns, or even entire tables that are not relevant.

实体模型是一个概念模型。您可以轻松地创建一组实体来暴露于一组客户端(可能是Web服务),另一组可映射到同一个数据库,这是针对不同的客户端(可能是Web应用程序)。

The entity model is meant to be a conceptual model. You can easily create a set of entities for exposure to one set of clients (web services, perhaps), and another set, mapping to the same database, that is meant for a different client (web application, perhaps).

另一方面,我总是建议不要透过Web服务暴露实体框架对象。不幸的是,Microsoft通过使用[DataMember]标记来实现与实现相关的属性。我刚刚尝试这样一个简单的服务,从AdventureWorks返回一个SalesOrderHeader。我的客户端接收到以下EF类型的代理版本:

On the other hand, I always recommend against ever exposing Entity Framework objects through a web service. Microsoft unfortunately exposes implementation-dependent properties by marking them with [DataMember]. I just now tried this with a simple service returning a SalesOrderHeader from AdventureWorks. My client received proxy versions of the following EF types:


  • EntityKeyMember

  • StructuralObject

  • EntityObject

  • EntityKey

  • EntityReference

  • RelatedEnd

  • EntityKeyMember
  • StructuralObject
  • EntityObject
  • EntityKey
  • EntityReference
  • RelatedEnd

这些不是客户需要知道的事情。

These are not things your clients need to know about.

我更喜欢暴露数据传输对象,并将属性从一个复制到另一个。显然,这比通过反思或代码生成更好。我已经通过代码生成过去(T4模板)。

I prefer exposing Data Transfer Objects, and copying the properties from one to the other. Obviously, this is better done through reflection or code generation, than by hand. I've done it through code generation in the past (T4 templates).

我还没有尝试过的一个选项是 AutoMapper

An option I haven't tried is AutoMapper.

这篇关于WCF,实体框架&数据合同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆