# Installation: Cannot open database "Hangfire"

**URL:** <https://discuss.hangfire.io/t/installation-cannot-open-database-hangfire/5204>\
**Category:** question\
**Created:** [September 13, 2018, 12:36am UTC](https://discuss.hangfire.io/t/installation-cannot-open-database-hangfire/5204 "2018-09-13T00:36:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gtovey](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@gtovey](https://discuss.hangfire.io/u/gtovey)\
**Post date:** [September 13, 2018, 12:36am UTC](https://discuss.hangfire.io/t/installation-cannot-open-database-hangfire/5204/1 "2018-09-13T00:36:31Z")

</div>

New to Hangfire

Trying to get Hangfire going in a .net core application.

1. I ran dotnet add package HangFire --version 1.7.0-beta2, that seemed to go fine
2. Created a connection string:  
‘“Hangfire”: “Server=localhost,1433;Database=Hangfire;User Id=sa;Password=xxxxxxxxxx;MultipleActiveResultSets=true”’
3. added `services.AddHangfire(s => s.UseSqlServerStorage(Configuration.GetConnectionString("Hangfire")));` to Startup.cs ConfigureServices()
4. added `app.UseHangfireDashboard();` and `app.UseHangfireServer();` to Startup.cs Configure()
5. when `app.UseHangfireDashboard();`runs it throws an error:

`Application startup exception: System.Data.SqlClient.SqlException (0x80131904): Cannot open database "Hangfire" requested by the login. The login failed. Login failed for user 'sa'.`

1. no Hangfire database exists or is created

2. When in the above process should the database be created/initialized?

3. Any ideas on this error?

4. Am I missing an installation step or steps?

Thanks,  
-Gary

---

<div class="post-metadata">

**Author:** ![mathew.dodson](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@mathew.dodson](https://discuss.hangfire.io/u/mathew.dodson)\
**Post date:** [September 13, 2018, 8:52pm UTC](https://discuss.hangfire.io/t/installation-cannot-open-database-hangfire/5204/2 "2018-09-13T20:52:32Z")

</div>

I believe it means it cannot connect to your database. Are those the correct login details for your db? I use localdb(sqlexpress) so I just need the basic windows authentication. Connect to the table that you want to connect to using visual studio and get the correct connection string there. That’s my thinking on the error.

---

<div class="post-metadata">

**Author:** ![Andrusza](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/andrusza/32/1423_2.png) [@Andrusza](https://discuss.hangfire.io/u/Andrusza)\
**Post date:** [October 23, 2018, 12:02pm UTC](https://discuss.hangfire.io/t/installation-cannot-open-database-hangfire/5204/3 "2018-10-23T12:02:30Z")

</div>

Hey did you manage to figure out what is wrong?  
I have same issue… I droped the database and now I cannot recreate it. Same error

---

<div class="post-metadata">

**Author:** ![gtovey](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@gtovey](https://discuss.hangfire.io/u/gtovey)\
**Post date:** [October 24, 2018, 2:42pm UTC](https://discuss.hangfire.io/t/installation-cannot-open-database-hangfire/5204/4 "2018-10-24T14:42:27Z")

</div>

We could not determine what the cause was. To get around it, we manually created an empty Hangfire database before re-attempting the above steps. Once we did that, and ran the steps, the tables and such were created in the Hangfire database.

---

<div class="post-metadata">

**Author:** ![Phat\_Hoang](https://avatars.discourse-cdn.com/v4/letter/p/5fc32e/32.png) [@Phat\_Hoang](https://discuss.hangfire.io/u/Phat_Hoang)\
**Post date:** [November 15, 2018, 3:59am UTC](https://discuss.hangfire.io/t/installation-cannot-open-database-hangfire/5204/5 "2018-11-15T03:59:18Z")

</div>

@gtovey  
Facing same issue with you. And I have just gone with a workaround solution.

1. Create an empty Context:

> public class HangfireContext : DbContext  
> {  
> public HangfireContext(DbContextOptions options) : base(options)  
> {  
> }  
> }

1. Run migrate before use HangfireServer

> // Migrate and seed the database during startup. Must be synchronous.  
> using (var serviceScope = app.ApplicationServices.GetRequiredService()  
> .CreateScope())  
> {  
> serviceScope.ServiceProvider.GetService().Database.Migrate();  
> }
> 
> app.UseHangfireServer();

---

<div class="post-metadata">

**Author:** ![wrwoodman](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/wrwoodman/32/1424_2.png) [@wrwoodman](https://discuss.hangfire.io/u/wrwoodman)\
**Post date:** [November 15, 2018, 9:47pm UTC](https://discuss.hangfire.io/t/installation-cannot-open-database-hangfire/5204/6 "2018-11-15T21:47:26Z")

</div>

Have a look at SqlServerStorageOptions. You should be able to set an option called PrepareSchemaIfNecessary and set it to true. We set ours to false as our process is to create all DBs before deploying code.

Hope that helps.

---

<div class="post-metadata">

**Author:** ![BFA](https://avatars.discourse-cdn.com/v4/letter/b/8baadc/32.png) [@BFA](https://discuss.hangfire.io/u/BFA)\
**Post date:** [February 22, 2019, 9:23pm UTC](https://discuss.hangfire.io/t/installation-cannot-open-database-hangfire/5204/7 "2019-02-22T21:23:56Z")

</div>

I tried the below, but it made no difference. Still get the same error:  
"An exception of type ‘System.Data.SqlClient.SqlException’ occurred in System.Data.SqlClient.dll but was not handled in user code  
Cannot open database “Hangfire” requested by the login. The login failed.  
"

```auto
var hangfireOptions = new SqlServerStorageOptions
{
      PrepareSchemaIfNecessary = true
 };
services.AddHangfire(config =>
{
    config.UseSqlServerStorage(@"Server=localhost; Database=Hangfire; Trusted_Connection=True;", hangfireOptions); 
});

```

Looks like a bug. Using version 1.6.22.
