I am developing a webjob in .net that will access and make changes to an associated web application recurrently. I have copied the db connection strings from the Web.config file on the web application to the App on the webjob application. The following code tries to access the database from the 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);
}
This line from the previous function
var vms = db.VMs.ToList();
Triggers the following error:
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'.
Next is the connection string both in App.config and Web.config. Right now I am running this webjob on my developer device so it should access a local database.
<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>
Can you spot anything wrong? The same db query does work in the MVC web application. I could not find any example online of a webjob accessing an EF database.
Assuming that you're running your application/webjob locally, then the problem is that you're pointing to a database that does not exist at that location. In essence, it's a connection string issue.
When you run the webjob locally, it's being executed from the bin/Debug folder (unless you provide some special configuration). Therefore, the code in the Entity Framework picks up the connection string from the app.config and tries to access it at the provided location. In your case, the application root. This is what the inner exception is telling you.
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'.
In order for this to work, you need to change the connection string to point to the correct database (.mdf) file in the file system.
If you want to test this out without changing the connection string, just run the webjob from the same place in the file system where the database is at; you can do a couple of things:
Also, my suggestion to you is to have a local version of SQL Server along with SSMS (SQL Server Management Studio) and install the database there. You're not going to have these issues if you do so.
If you're going to deploy this to Azure (and still use that portable database), then make sure you adjust the connection string in the WebJob since I'm assuming you're going to run into a similar issue.
Hope this helps,