Return Entity Framework objects from WCF

别说谁变了你拦得住时间么 提交于 2019-12-07 14:52:58

问题


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.

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).

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();

回答1:


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

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.

Here is a related answer: https://stackoverflow.com/a/7161377/7720




回答2:


You probably want to send DTOs across the wire rather than your EF objects.

You could use something like AutoMapper to populate your DTOs from the EF objects.



来源:https://stackoverflow.com/questions/16791169/return-entity-framework-objects-from-wcf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!