DataServiceContext可以维护自己的实体集吗? [英] Can DataServiceContext maintain its own Entity set?

查看:50
本文介绍了DataServiceContext可以维护自己的实体集吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试使用Azure桌面服务的ADO.NET数据服务客户端。 Azure表批处理查询的一个限制是它们一次只能更新100个实体,并且实体必须都具有相同的PartitionKey。


我一直在努力解决如何更改DataServiceContext的子类,以便在调用SaveChanges方法时,类将按PartitionKey对跟踪的实体进行排序,并将它们拆分为具有相同PartitionKey和Max 100实体的组,然后将它们添加到新的DataServiceContext并保存批处理。


似乎DataServiceContext的Entities属性是一个只读集合,它包含EntityDescriptor对象。


任何人都可以建议明智的实现这个的方法?

另外我可以改变我的代码为每个独特的partitionKey生成一个新的DataServiceContext(我已经完成了这个并且它工作正常)但是似乎没有任何办法防止实体更新增长大于100(这会导致Azure表拒绝b atch),因为任何代码都可以对被跟踪的实体进行更新。

由于Azure表的延迟,尽可能多地进行批量处理是值得的,因为它可以显着影响性能,但是我我不确定最好的方法。有没有人有任何建议?

谢谢,
Stevo

解决方案

Hi Stevo,

向DataServiceContext添加新实体的好方法是在DataServiceContext上使用AttachTo / Detach方法。在您的情况下,您将首先从原始DataServiceContext分离实体。然后,您将在新的DataServiceContext上使用AttachTo。
如果基于PartitionKey移动,则新的DataServiceContext将具有具有相应PartitionKeys的所有实体。目前,当您分离实体时,DataServiceContext不会跟踪相应的EntityDescriptor。当你调用
AttachTo时,EntityDescriptor会按照我们的约定重新创建。我不完全确定我们的约定是否与Azure表服务匹配,因此这可能需要考虑。在我们这方面,我们一直在考虑将EntityDescriptor保持在
左右,并且可能会在未来的WCF数据服务版本中进行更改。


还有多种方法可以获得100个实体取决于您的方案。如果您只关心DataServiceContext有100个实体,则可以在调用AttachTo之前检查Entities属性。如果已经有100个实体,请停止附加到
新的DataServiceContext。

如果您不关心被跟踪实体的总数,但是对拥有100个已修改实体更感兴趣,则可以在DataServiceContext的子类中进行一些更改。例如,您可以通过检查Entities属性中的EntityDescriptor.State来编写查询以检查已更改实体的数量
。如果修改实体的数量小于或等于99,您可以允许用户更新实体。


谢谢,

Julian


Hi,

I am experimenting with ADO.NET Data services client for Azure table services. One of the restrictions of Azure tables batch queries is that they can only update 100 entities at a time, and that the entities must all have the same PartitionKey.

I have been trying to work out how to alter a subclass of a DataServiceContext so that when a SaveChanges method is called, the class will sort the tracked entities by PartitionKey and split them into groups with the same PartitionKey and Max 100 entities, then adding them to a new DataServiceContext and saving the batch.

It seems that the Entities property of DataServiceContext is a read only collection, and that is contains EntityDescriptor objects.

Can anyone suggest a sensible approach to achieve this?

Alternatively I can alter my code to generate a new DataServiceContext for each unique partitionKey (I've done this and it works fine), however there doesn't seem to be any way to prevent the Entity updates growing larger than 100 (which causes Azure tables to reject the batch), as any code can make an update to a tracked Entity.

With the latency of Azure tables, it's worthwhile getting as much batched as possible, as it can significantly affect the performance, however I'm not sure on the best approach. Has anyone got any suggestions?

Thanks,
Stevo

解决方案

Hi Stevo,
A good way to add new entities to a DataServiceContext is to use the AttachTo/Detach methods on the DataServiceContext. In your case, you would first detach the entity from the original DataServiceContext. Then, you would AttachTo on the new DataServiceContext. If you moved based on PartitionKey, the new DataServiceContext would have all the entities with corresponding PartitionKeys. Currently when you detach an entity, the DataServiceContext doesn't keep track of the corresponding EntityDescriptor. When you call AttachTo, the EntityDescriptor is recreated following our convention. I am not completely sure if our convention matches Azure table services so this may be something to look into. On our side, we have been thinking about keeping the EntityDescriptor around and it may be changed in future releases of WCF Data Services.

There are also multiple ways to get 100 entities depending on your scenario. If you only care that a DataServiceContext has 100 entities, you could check the Entities property before calling AttachTo. If there are already 100 entities, stop attaching to the new DataServiceContext.
If you do not care about the total number of tracked entities but are more interested in having 100 modified entities, you could make some changes in your subclass of DataServiceContext. For example, you could write a query to check the number of changed entities by checking the EntityDescriptor.State in the Entities property. If the number of modified entities is less than or equal to 99, you can allow the user to update an entity.

Thanks,
Julian


这篇关于DataServiceContext可以维护自己的实体集吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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