从WCF返回实体框架对象 [英] Return Entity Framework objects from WCF
问题描述
我正在使用WCF服务来向多个移动客户端提供数据。数据模型是Entity Framework 4.0。模式如下:
I am working on a WCF service to provide data to multiple mobile clients. Data model is Entity Framework 4.0. Schema is given below.
当我重试SysUser的对象时,结果还包含导航属性和EntityKey等EF相关的东西。有可能我得到纯对象(只有数据库字段没有关系等)。
When i returnt he object of SysUser the result also contains the navigation properties and EntityKey and other EF related stuff. Is it possible that i get the pure object(only the database fields without the relationship etc).
感谢
更新
异常发生仅LINQ to Entities支持无参数构造函数和初始值设置。在以下代码:
Thanks Update the exception occures "Only parameterless constructors and initializers are supported in LINQ to Entities." on followign code:
return (from u in DataSource.SysUsers
where u.UserID == UserID
select new Player(u)
).FirstOrDefault();
推荐答案
我想如果您删除
关键字,导航属性将不会加载。之后,如果您需要加载此属性,可以手动执行此操作: http ://msdn.microsoft.com/en-us/data/jj574232 SysUser
中的虚拟
I think if you remove the virtual
keyword in your SysUser
model for the navigation properties, those will not be loaded. Later, if you need to load this properties, you can do it manually as stated here: http://msdn.microsoft.com/en-us/data/jj574232
现在,如果要使 SysUser
通过WCF服务旅行,这不是一个好主意。首先,您的服务的客户端将需要参考您的模型项目,而这并不适合您。如果你不参考你的模型,你会得到一个代理,这或多或少与Joe R对DTO的解释相同。
Now, if you want to make SysUser
travel through a WCF service, it is not a good idea. First, your service's client will need a reference to your Models Project... and that doesn't feels right. If you don't reference your Models, you will get a proxy for it, that is more or less the same as Joe R explained about DTOs.
以下是相关答案: https://stackoverflow.com/a / 7161377/7720
这篇关于从WCF返回实体框架对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!