ASP.NET Webjobs 访问 Web 应用程序 EF 数据库时出错
ASP.NET Webjobs error accessing the web application EF database
我正在开发一个 webjob in .net,它将周期性地访问和更改关联的 Web 应用程序。我已将 db 连接字符串从 Web 应用程序上的 Web.config 文件复制到 Webjob 应用程序上的 App。以下代码尝试从 webjob 访问数据库。
Functions.cs:
// This function will be triggered based on the schedule you have set for this WebJob
// This function will enqueue a message on an Azure Queue called queue
[NoAutomaticTrigger]
public static void ManualTrigger(TextWriter log, int value, [Queue("queue")] out string message)
{
var db = new ApplicationDbContext();
var vms = db.VMs.ToList();
var a = 1;
log.WriteLine("Function is invoked with value={0}", value);
message = value.ToString();
log.WriteLine("Following message will be written on the Queue={0}", message);
}
这一行来自上一个函数
var vms = db.VMs.ToList();
触发以下错误:
An exception occurred while initializing the database. See the InnerException for details
Inner exception message: Cannot attach the file 'C:\Users\ernestbofill\Source\Repos\biocloud\site\TimeLeftJob\bin\Debug\aspnet-site-20150515045532.mdf' as database 'aspnet-site-20150515045532'.
接下来是 App.config 和 Web.config 中的连接字符串。现在我在我的开发者设备上 运行 这个 webjob,所以它应该访问本地数据库。
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-site-20150515045532.mdf;Initial Catalog=aspnet-site-20150515045532;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
你能发现什么不对吗?相同的数据库查询在 MVC Web 应用程序中确实有效。我无法在网上找到任何访问 EF 数据库的 Web 作业示例。
假设您 运行 在本地安装 application/webjob,那么问题是您指向的数据库在该位置不存在。本质上,这是一个连接字符串问题。
当您在本地 运行 webjob 时,它是从 bin/Debug 文件夹执行的(除非您提供一些特殊配置)。因此,Entity Framework 中的代码从 app.config 中获取连接字符串,并尝试在提供的位置访问它。在您的情况下,应用程序根目录。这就是内部异常告诉你的。
Inner exception message: Cannot attach the file 'C:\Users\ernestbofill\Source\Repos\biocloud\site\TimeLeftJob\bin\Debug\aspnet-site-20150515045532.mdf' as database 'aspnet-site-20150515045532'.
为了使其正常工作,您需要更改连接字符串以指向文件系统中正确的数据库 (.mdf) 文件。
如果您想在不更改连接字符串的情况下进行测试,只需 运行 来自数据库所在文件系统中同一位置的 webjob;你可以做几件事:
- 将 .mdf 文件复制到网络作业的 bin/Debug 文件夹中。
- 将 bin/Debug 文件夹的内容复制到数据库所在的位置并执行程序。
此外,我建议您使用本地版本的 SQL Server 以及 SSMS(SQL Server Management Studio)并在其中安装数据库。如果这样做,您将不会遇到这些问题。
如果您要将其部署到 Azure(并且仍然使用该便携式数据库),请确保调整 WebJob 中的连接字符串,因为我假设您要 运行进入类似的问题。
希望这对您有所帮助,
我正在开发一个 webjob in .net,它将周期性地访问和更改关联的 Web 应用程序。我已将 db 连接字符串从 Web 应用程序上的 Web.config 文件复制到 Webjob 应用程序上的 App。以下代码尝试从 webjob 访问数据库。
Functions.cs:
// This function will be triggered based on the schedule you have set for this WebJob
// This function will enqueue a message on an Azure Queue called queue
[NoAutomaticTrigger]
public static void ManualTrigger(TextWriter log, int value, [Queue("queue")] out string message)
{
var db = new ApplicationDbContext();
var vms = db.VMs.ToList();
var a = 1;
log.WriteLine("Function is invoked with value={0}", value);
message = value.ToString();
log.WriteLine("Following message will be written on the Queue={0}", message);
}
这一行来自上一个函数
var vms = db.VMs.ToList();
触发以下错误:
An exception occurred while initializing the database. See the InnerException for details
Inner exception message: Cannot attach the file 'C:\Users\ernestbofill\Source\Repos\biocloud\site\TimeLeftJob\bin\Debug\aspnet-site-20150515045532.mdf' as database 'aspnet-site-20150515045532'.
接下来是 App.config 和 Web.config 中的连接字符串。现在我在我的开发者设备上 运行 这个 webjob,所以它应该访问本地数据库。
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-site-20150515045532.mdf;Initial Catalog=aspnet-site-20150515045532;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
你能发现什么不对吗?相同的数据库查询在 MVC Web 应用程序中确实有效。我无法在网上找到任何访问 EF 数据库的 Web 作业示例。
假设您 运行 在本地安装 application/webjob,那么问题是您指向的数据库在该位置不存在。本质上,这是一个连接字符串问题。
当您在本地 运行 webjob 时,它是从 bin/Debug 文件夹执行的(除非您提供一些特殊配置)。因此,Entity Framework 中的代码从 app.config 中获取连接字符串,并尝试在提供的位置访问它。在您的情况下,应用程序根目录。这就是内部异常告诉你的。
Inner exception message: Cannot attach the file 'C:\Users\ernestbofill\Source\Repos\biocloud\site\TimeLeftJob\bin\Debug\aspnet-site-20150515045532.mdf' as database 'aspnet-site-20150515045532'.
为了使其正常工作,您需要更改连接字符串以指向文件系统中正确的数据库 (.mdf) 文件。
如果您想在不更改连接字符串的情况下进行测试,只需 运行 来自数据库所在文件系统中同一位置的 webjob;你可以做几件事:
- 将 .mdf 文件复制到网络作业的 bin/Debug 文件夹中。
- 将 bin/Debug 文件夹的内容复制到数据库所在的位置并执行程序。
此外,我建议您使用本地版本的 SQL Server 以及 SSMS(SQL Server Management Studio)并在其中安装数据库。如果这样做,您将不会遇到这些问题。
如果您要将其部署到 Azure(并且仍然使用该便携式数据库),请确保调整 WebJob 中的连接字符串,因为我假设您要 运行进入类似的问题。
希望这对您有所帮助,