WCF,实体框架4.1和实体状态 [英] WCF, Entity Framework 4.1 and Entity's State

本文介绍了WCF,实体框架4.1和实体状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个暴露WCF服务的应用程序。我正在DAL使用Entity Framework 4.1。问题是当我加载一些实体(让一个客户有一个Order而又有OrderDetail)。加载后,我在Customer,Order和OrderDetail对象中进行了一些更改(添加了一些新订单,并删除/更新了一些现有订单),并将对象图发送到WCF服务以更新如下。

 客户oCustomer; 
using(var context = new MyContext)// MyContext是dbContext
{
oCustomer = context.Include(Order.OrderDetail)Find(1);
}

oCustomer.Name =blah blah;
订单oOrder1 = oCustomer.Order.Where(obj => obj.Id == 2);
oOrder1.Description =blah blah;
oOrder1.OrderDetail.Quantity = 10;

订单oOrder2 = new Order {...} //设置Order的属性。
oCustomer.Order.Add(oOrder2);

oCustomer.Order.Remove(context.Order.Find(1));

ServiceClient client = new ServiceClient();
client.SaveCustomer(oCustomer);

现在,当我在服务器端收到客户的更新的ObjectGraph时,我不知道哪个订单已被删除这是修改,因为现在没有变化。如何确定哪个订单要删除以及要修改的订单?有没有办法跟踪Entity Framework 4.1中对象图中的变化?



注意:以前我使用了自我跟踪实体来解决这个问题,但是我必须摆脱的STE,因为我的WCF服务也将被Java客户端应用程序使用。因此,STE不是我的选择。

解决方案

据了解,没有办法跟踪分离对象的更改图表除了自我跟踪实体(您不能像您说的那样)。



我通常会更新分离的图形,是从原始图形重新加载数据库,比较原来与改变图(手写代码逐案),看看哪些实体被添加,​​修改和删除,将更改写入原始文件并保存上下文。



此过程的示例(对于一个父级和子级集合,例如Customer和Order集合)在此问题的答案中:关系不能改变,因为一个或多个外键属性是不可空



不幸的是,如果您必须处理更深层次的实体,则相当复杂。据我所见,EF不会将改变后的图形自动合并到原始内容中,只适用于实体只具有已更改的标量和复杂属性的非常简单的情况。一旦涉及导航属性,您就不再需要EF的支持,您必须自己编写更新逻辑。


I am developing an application which will expose WCF services. I am using Entity Framework 4.1 at the DAL. The problem is when I load some entity (let say a Customer that has Order which in turn has OrderDetail). After loading it I make some changes in Customer, Order and OrderDetail objects (some new orders are added and some existing orders are removed/updated) and send the object graph to WCF service to update it as following.

Customer oCustomer;
using(var context = new MyContext) //MyContext is dbContext
{
    oCustomer = context.Include("Order.OrderDetail").Find(1);
}

oCustomer.Name ="blah blah";
Order oOrder1 = oCustomer.Order.Where(obj=>obj.Id == 2);
oOrder1.Description = "blah blah";
oOrder1.OrderDetail.Quantity = 10;

Order oOrder2 = new Order { ... } //properties of Order are set.
oCustomer.Order.Add(oOrder2);

oCustomer.Order.Remove(context.Order.Find(1));

ServiceClient client = new ServiceClient();
client.SaveCustomer(oCustomer);

Now when I receive the updated ObjectGraph of Customer at Server side I don't know which Order was removed and which was modified, since there is no changetracking now. How can I determine that which Order to Delete and Which Order to Modify? Is there any way to track the changes in my object graph in Entity Framework 4.1?

NOTE: Previously I used Self Tracking Entities which solved this problem but I had to get rid of STEs since my WCF service is gonna be used by Java Client apps as well. So, STEs are not an option for me.

解决方案

As far as know there is no way to track changes of a detached object graph aside from Self Tracking Entities (which you can't use as you say).

What I usually do to update a detached graph is to reload the original graph from the database, compare the original with the changed graph (hand-written code case by case) to see which entities have been added, modified and deleted, write the changes into the original and save the context.

An example to this procedure (for one parent and a child collection, for instance Customer and Order collection) is in the answers to this question: The relationship could not be changed because one or more of the foreign-key properties is non-nullable

It's unfortunately getting quite complex if you have to deal with a deeper hierarchy of entities. As far as I can see EF doesn't offer any kind of automatic merging of a changed graph into the original, only for the very simple case where an entity only has scalar and complex properties which have been changed. As soon as navigation properties are involved you have no support from EF anymore and you have to write the update logic yourself.

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

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