本文介绍了如何防止.NET实体中的私有属性通过服务公开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个传输通过实体框架创建的实体对象的WCF服务。我有一个用户实体映射到用户数据库表。有些用户字段(密码,DateCreated等),我不想公开给客户端,但是由于它们在数据库中不可空,Visual Studio需要映射。将这些属性设置为私有似乎是一个很好的解决方法,但是这些属性在客户端使用时会转换为public。

I'm creating a WCF service that transfers entity objects created via entity framework. I have a User entity that maps to a User db table. There are certain User fields (Password, DateCreated, etc) that I don't want to expose to the client but, because they are non-nullable in the db, Visual Studio requires mappings. Setting these properties as private seems like a good workaround but these properties are converted to public when consumed by a client.

有没有办法解决这个问题,或者更好的方法采取?我宁愿避免在数据库级别更改这些字段,只是为了使EF快乐。

Is there a way around this, or a better approach to take? I'd rather avoid changing these fields at the db level just to make EF happy.

推荐答案

您可以随时实现,而只能通过线路发送的属性。

Either that, or if you can, add the DataContract attribute to the type, and the DataMember attribute to only the properties you wish to send over the wire.

这篇关于如何防止.NET实体中的私有属性通过服务公开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 17:55