15

I am using Hangfire in an ASP.NET MVC5 application to send an email. Until now, it was working fine, but now when I run the application, it is throwing this error:

There is already an object named 'Job' in the database.nstalling Hangfire SQL objects... Database schema [HangFire] already exists Table [HangFire].[Schema] already exists.. Installing schema version 1

From this error, I understood that Hangfire trying to create all those tables in the database which are already there. But I'm not getting reason behind it because till now application was working fine, even when I run it locally many times.

halfer
  • 19,824
  • 17
  • 99
  • 186
Dreamer
  • 586
  • 3
  • 7
  • 23
  • 2
    This can be a bug, you can disable the process to create the table by using this var options = new SqlServerStorageOptions { PrepareSchemaIfNecessary = false }; GlobalConfiguration.Configuration.UseSqlServerStorage("", options); – Kim Hoang Dec 22 '16 at 02:15
  • This is a really annoying error where hangfire tries to recreate itself after the application restarts even though it is already created. The fix suggested above does not work when you are doing a fresh deploy – johnstaveley Dec 01 '17 at 10:53

4 Answers4

12

This happened to me and I run this query and restart the server. Just run the query:

INSERT INTO [HangFire].[Schema]
           ([Version])
     VALUES
           (5)
Tobias Wilfert
  • 919
  • 3
  • 14
  • 26
tripurary
  • 121
  • 1
  • 4
7

This happened to me when the the sql database user did not have the proper permission in the database (db_datareader, db_ddladmin, db_datawriter, etc).

Adding the proper permission solves it immediately.

Rosdi Kasim
  • 24,267
  • 23
  • 130
  • 154
  • This was the case with me also. I restored development DB, which didn't have AppPool user allowed. After I added the user, I forgot to add it to `datawriter` and `datareader` membership. So this solved the problem. – gtu Jan 29 '22 at 10:49
7

I got that problem after export\import the database via .bacpac finished with errors. I removed all my hangfire tables on development database and it worked (careful, do not do that on production DB).

DROP TABLE IF EXISTS HangFire.AggregatedCounter
DROP TABLE IF EXISTS HangFire.Counter
DROP TABLE IF EXISTS HangFire.Hash
DROP TABLE IF EXISTS HangFire.JobParameter
DROP TABLE IF EXISTS HangFire.JobQueue
DROP TABLE IF EXISTS HangFire.List
DROP TABLE IF EXISTS HangFire.[Schema]
DROP TABLE IF EXISTS HangFire.Server
DROP TABLE IF EXISTS HangFire.[Set]
DROP Table IF EXISTS HangFire.State
DROP TABLE IF EXISTS HangFire.Job
Liam Kernighan
  • 2,335
  • 1
  • 21
  • 24
4

My problem was solved by the following Code In Asp Core 2.2:

Startup Class --> ConfigureServices Method:

  var cfg = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .Build();

        services.AddHangfire(x => x
            .UseSqlServerStorage(
                cfg.GetConnectionString("DefaultConnection"),
                new SqlServerStorageOptions
                {
                    PrepareSchemaIfNecessary = false
                }
            ));

appsetting.json File:

 "ConnectionStrings": {
    "DefaultConnection": "Data Source=.;Initial Catalog=databaseName;Persist Security Info=True;User ID=sa;Password=123;MultipleActiveResultSets=True;Application Name=Hang-fire"
  },
Amin Golmahalleh
  • 3,585
  • 2
  • 23
  • 36
  • 1
    Or in .NET MVC: GlobalConfiguration.Configuration.UseSqlServerStorage(KsefHangFireHelper.ConnectionString, new SqlServerStorageOptions { PrepareSchemaIfNecessary = false }); – Marcin Feb 02 '23 at 09:57