Getting SQL Server error while using Hangfire
Asked Answered
Z

4

16

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.

Zoospore answered 16/12, 2016 at 4:19 Comment(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("<name or connection string>", options);Despair
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 deployBorras
M
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)
Mucilage answered 6/12, 2018 at 10:13 Comment(1)
Thanks. It's work well for error HangFire.Job can not created. Invalid name HangFire.JobGraaf
B
8

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.

Barnsley answered 19/11, 2017 at 8:31 Comment(1)
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.Lapointe
T
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
Therontheropod answered 12/4, 2019 at 12:49 Comment(0)
P
5

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"
  },
Procaine answered 14/12, 2019 at 11:43 Comment(1)
Or in .NET MVC: GlobalConfiguration.Configuration.UseSqlServerStorage(KsefHangFireHelper.ConnectionString, new SqlServerStorageOptions { PrepareSchemaIfNecessary = false });Brigidabrigit

© 2022 - 2024 — McMap. All rights reserved.