使用实体框架进行存储过程即发即忘
我在应用程序中使用实体框架 4.1。要求之一是在数据库上执行一些存储过程,其中有些需要相当长的时间。此外,这些存储过程不会返回任何结果,因此我只需启动它们并忘记它们。
当然,.NET 会等待这些操作完成,因此一段时间后它会抛出“超时期限已过期”的异常。
我知道我可以通过将 CommandTimeout 属性设置为更高的值来解决这个问题,但是我正在寻找替代解决方案(如果存在的话)。
是否可以使用实体框架作为“即发即忘”来执行存储过程?
任何帮助将不胜感激。
问候
I am using the Entity Framework 4.1 within an application. One of the requirments is to execute some stored procedures on the database out of which some take quite some time. Further, those stored procedures do not return any results so I need to only start them and forget about them.
Naturally, .NET will wait for these operations to complete so after some time it throws an exception that the "Timeout period has expired".
I know that I could fix that by setting the CommandTimeout property to a higher value, however I am looking for an alternative solution (If such even exists).
Is it possible to execute stored procedures using the Entity Framework as Fire-and-Forget?
Any help will be appreciated.
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
存储过程不支持即发即忘执行。您可以使用普通的 ADO.NET 并在单独的连接上异步执行查询(使用
BeginExecuteNonQuery
)。 EF不支持异步执行。另一种更复杂的方式,其行为类似于即发即忘,是通过单步执行存储过程来创建 SQL 作业。您将调用sp_start_job
,而不是调用您的存储过程,它在启动作业后立即返回,并且作业将异步执行,而不将任何其他结果返回给您的应用程序。Stored procedures don't support fire and forget execution. You can either use plain ADO.NET and execute query asynchronously on separate connection (with
BeginExecuteNonQuery
). EF doesn't support asynchronous execution. Another more complex way which behaves like fire and forget is creating SQL Job with single step execution your stored procedure. Instead of calling your stored procedure you will callsp_start_job
which returns immediately after starting the job and job will execute asynchronously without returning any other result back to your application.