Using Hangfire, the connection string specified in Startup.cs throws Cannot bind file to database error

I use Hangfire in my ASP.Net MVC Web App, it has been successfully installed. I would like to use the same LocalDb to hold jobs in the queue for Hangfire, to delete and process as I used to store data. However, I ran into the error below when I presented my connectionString or the name defined in Web.config in Startp.cs . I had no problems adding, removing data updates in the same localDb before putting on the flash.

 Cannot attach the file 'c:\users\jerry_dev\documents\visual studio 2013\Projects\Hangfire.Highlighter\Hangfire.Highlighter\App_Data\aspnet-Hangfire.Highlighter-20150113085546.mdf' as database 'aspnet-Hangfire.Highlighter-20150113085546'. Startup.cs: public void Configuration(IAppBuilder app) { ConfigureAuth(app); app.UseHangfire(config => { string hangfireConnectionString = @"Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-Hangfire.Highlighter-20150113085546.mdf;Initial Catalog=aspnet-Hangfire.Highlighter-20150113085546;Integrated Security=True"; config.UseSqlServerStorage(hangfireConnectionString); config.UseServer(); }); } 

My project solution is called "Hangfire.Highlighter"

 Web.config: <connectionStrings> <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-Hangfire.Highlighter-20150113085546.mdf;Initial Catalog=aspnet-Hangfire.Highlighter-20150113085546;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> 
+10
c # connection-string hangfire
source share
4 answers

I know that this is old - but 9 months have passed already, and I also pulled out my hair - and decided to write about it here .

My solution was to simply create a quick and dirty DbContext, tell it the correct connection string and call Database.CreateIfNotExists in the constructor:

 public class HangfireContext : DbContext { public HangfireContext() : base("name=HangfireContext") { Database.SetInitializer<HangfireContext>(null); Database.CreateIfNotExists(); } } 

In the HangfireBootstrapper.Start () method, I am doing something like this:

 public void Start() { lock (_lockObject) { if (_started) return; _started = true; HostingEnvironment.RegisterObject(this); //This will create the DB if it doesn't exist var db = new HangfireContext(); GlobalConfiguration.Configuration.UseSqlServerStorage("HangfireContext"); // See the next section on why we set the ServerName var options = new BackgroundJobServerOptions() { ServerName = ConfigurationManager.AppSettings["HangfireServerName"] }; _backgroundJobServer = new BackgroundJobServer(options); var jobStarter = DependencyResolver.Current.GetService<JobBootstrapper>(); //See the Recurring Jobs + SimpleInjector section jobStarter.Bootstrap(); } } 

You don’t know why Hangfire has such difficulties with LocalDb - maybe it can only process full SQL instances? In any case, this works for me, the new team members and the new dev / staging / prod instances that got up.

+18
source share

I also know that this is old, but I have come across this recently. Here is my fix:

  • In Visual Studio, go to "View -> SQL Server Explorer"
  • Connect to the data source if it is not already connected. In the above example, it was (LocalDb) \ v11.0 '
  • Right-click Databases β†’ Add New Database
  • Fill in the database name = Ex: 'aspnet-Hangfire.Highlighter-20150113085546' or as you called the database in the connection string.
  • Fill in the database location = This should be the data directory in your application, "App_Data" for the MVC project.

This fixed the problem in my case.

+2
source share

Is a database already created? Can you try using a different conneciton string format? Something like this, "Server = .; Database = HangFire.Highlighter; Trusted_Connection = True;"

0
source share

Jack's answer did not work for me because I ran into this problem: Not a single line of communication string was found in the application configuration file

I got it to work with the following changes:

  • Remove "name=" from the string in the base initializer. Thanks: stack overflow
  • This will result in an error when calling UseSqlServerStorage . Therefore, instead of passing the "HangfireContext" to it, I just copy the connection string from the database context.

Full installation code:

 public class HangfireContext : DbContext { public HangfireContext() : base("HangfireContext") // Remove "name=" { Database.SetInitializer<HangfireContext>(null); Database.CreateIfNotExists(); } } public partial class Startup { public static void ConfigureHangfire(IAppBuilder app) { var db = new HangfireContext(); GlobalConfiguration.Configuration.UseSqlServerStorage(db.Database.Connection.ConnectionString); // Copy connection string app.UseHangfireDashboard(); app.UseHangfireServer(); } } 
0
source share

All Articles