WCF 数据服务和实体框架代理对象
我有一个关于 WCF DataService 和 Entity Framework 4.1(代码优先) 的问题。 所以我在 Web 服务器上有一个 DataService:
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class CrmDataService : DataService<CrmDataContext>
{
private static CrmDataContext _mdc;
public static void InitializeService(DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
config.UseVerboseErrors = true;
}
protected override CrmDataContext CreateDataSource()
{
_mdc = new CrmDataContext(@"Data Source=localhost;Database=MyDb;User Id=sqluser;Password=111111;") { TablePrefix = "crm_" };
_mdc.Configuration.ProxyCreationEnabled = false;
return _mdc;
}
我还有一个 CrmDataContext 使用的实体对象列表(例如公司、地址、个人等) 将此服务添加到我的客户端应用程序(例如服务命名空间)后,我得到了相同的实体对象,但位于服务命名空间中。当然,然后我想通过数据服务获取任何 Company 对象(例如),它从命名空间 Services 返回一组实体对象。
所以我的问题是如何告诉数据服务使用我的真实实体对象并且不在我的项目中创建这些其他代理对象? 如果不可能,那么如何将从数据服务中获取的对象复制到我的真实实体中?
我的目标是使用数据上下文通过数据服务从服务器获取一些实体对象,然后在客户端获取相同的实体对象。我想对本地和服务器端的所有实体对象使用一个程序集。
I have a question regarding WCF DataService and Entity Framework 4.1 (code-first).
So I have a DataService on web server:
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class CrmDataService : DataService<CrmDataContext>
{
private static CrmDataContext _mdc;
public static void InitializeService(DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
config.UseVerboseErrors = true;
}
protected override CrmDataContext CreateDataSource()
{
_mdc = new CrmDataContext(@"Data Source=localhost;Database=MyDb;User Id=sqluser;Password=111111;") { TablePrefix = "crm_" };
_mdc.Configuration.ProxyCreationEnabled = false;
return _mdc;
}
I also have a list of entity objects used by my CrmDataContext (such as Company, Address, Person etc.)
After adding this service into my client application (into Services namespace e.g.) I got the same entity objects but in the Services namespace. And of course then I want to get any Company object (for example) through Data Service it returns me a set of entity objects from the namespace Services.
So my question is how can I tell data service to use my real entity objects and do not create these other proxy objects in my project?
If it is not possible to do, so how can I copy the objects that I get from data service into my real entities?
My goal is to get some entity objects from server through data service using data context and than same them on a client side. I want to use one assembly for all entity objects both in local and server sides.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想使用相同的对象,则无需将服务添加到客户端应用程序中。只需将包含类型的程序集添加到引用的程序集中,然后在客户端应用程序中,使用服务 uri 创建 DataServiceContext。
您必须执行以下操作:
context.CreateQuery(entitysetName)。
T 是您在服务和客户端中使用的常见类型。
要记住的一件事是,如果实体中的键不遵循约定,您可能必须在类型上添加 DataServiceKeyAttribute 或 DataServiceEntityAttribute。
希望这有帮助。
谢谢
普拉蒂克
If you want to use the same objects, then you do not need to add the service into the client application. Just add the assembly containing the types into the referenced assembly, and in the client app, create the DataServiceContext with the service uri.
You will have to do something like this:
context.CreateQuery(entitysetName).
T is the common type that you use across service and client.
One thing to keep in mind, if the keys in the entity do not follow the convention, you might have to add the DataServiceKeyAttribute or DataServiceEntityAttribute on the type.
Hope this helps.
Thanks
Pratik