从线程从Role.OnStart催生EF使用() [英] EF usage from thread spawned from Role.OnStart()

查看:174
本文介绍了从线程从Role.OnStart催生EF使用()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用EF code-先来管理我的DB连接,在web.config中宣布一个明确的连接字符串。我想安排一些DB清洗过程中(如删除每天的测试数据),所以我从产卵Role.OnStart()线程适当并发管理实例之一。

I'm using EF code-first to manage my DB connection, with an explicit connection string declared in web.config. I would like to schedule some DB cleaning process (like deleting test transactions every day), so I'm spawning a thread from Role.OnStart() with proper concurrency management among the instances.

但我发现了DB数据库异常,如DB不匹配我的模型,而我敢肯定它(从内部的应用效果很好使用相同的code)。所以我的猜测是,web.config文件不是从线程使用,所以EF可能使用默认的连接字符串。

But I'm getting DB database exceptions, like the DB not matching my model, whereas I'm sure it does (the same code used from "inside" the app works well). So my guess is that web.config is not used from the thread, so EF probably uses the default connection string.

什么是用我的连接字符串从那里最好?

What would be the best way to use my connection string from there ?

感谢

推荐答案

OnStart方法不能在同一个进程作为Web应用程序运行这意味着它不会使用在web.config中。我建议你​​存储连接字符串中的服务配置和初始化上下文时,从这里读它。

The OnStart method doesn't run in the same process as your web application meaning it doesn't make use of the web.config. I suggest you store the connection string in the service configuration and read it from here when initializing your context.

这是另一个优点是,您更改设置,而无需重新部署应用程序。

An other advantage is that you change the setting without re-deploying the application.

这篇关于从线程从Role.OnStart催生EF使用()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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