实体框架 - 将异常结果保存到数据库 [英] Entity Framework - Saving results to the database asyncronously

查看:111
本文介绍了实体框架 - 将异常结果保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web服务,每隔几秒就会调用一次,这样做一大堆东西,然后执行大量插入,目前平均需要半秒钟才能调用SaveChanges()。我想保存所有的更改异步,因为我可以每秒调用我的web服务多次,只需缓冲任何数据库更改,以保存由石英计时器,这将导致更好的性能。



我观察到在执行了Web服务功能后,如果在实体数据库模型对象上没有调用SaveChanges,任何挂起的数据库更改都将被滚动背部。



我可以异步地将我的更改保存到数据库,我该怎么做?

解决方案

原则上,您可以使用.NET中任何异步方法用于此目的。如果你可以使用.NET 4.5,那么建议你看看async / await方法,因为它们很容易使用。实体框架不支持async /等待从框,但您可以添加您的简单包装器使用 TaskCompletionSource 类。


I have a web-service which gets called every few seconds, which does a bunch of things and then performs a bulk insert which currently takes half a second on average to call SaveChanges(). I'd like to save all the changes asynchronously because then I can call my web-service multiple times per second and just buffer any database changes to be saved by a quartz timer, which should lead to better performance.

I've observed that after a web-service function executes, if the "SaveChanges" was not called on the entity database model object, any pending database changes will be rolled back.

Is it possible for me to asynchronously save my changes to the database and how would I go about doing it?

解决方案

In principle, you could use any of asynchronous approaches available in .NET for such purpose. If you could use .NET 4.5 then I'd suggest you to take a look at async/await methods, because they're quite easy to use. Entity framework doesn't support async/await from the box, but you could add your simple wrappers by using TaskCompletionSource class.

这篇关于实体框架 - 将异常结果保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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