Getting SQL Server error while using Hangfire - asp.net-mvc-5

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.

This happened to me and I run this query and restart the server.
Just run the query:
INSERT INTO [HangFire].[Schema]
([Version])
VALUES
(5)

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.

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

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"
},

Related

Oracle: Why am I getting Error: ORA-00903: invalid table name when database table exists?

Background
I have a NodeJS server powered by ExpressJS that fetches data from Oracle database. I manage the database with Oracle SQL Developer tool. There is a database called MCF01 created using Database Configuration Assistant with pluggable database also named MCF01.
I connect to it using SYSTEM and the admin password both inside Oracle SQL Developer tool and from my server:
Now I created a table named CM_ITM_ALL:
CREATE TABLE "SYS"."CM_ITM_ALL" ...
I can without a problem query inside CM_ITM_ALL from Oracle SQL Developer tool like so:
Problem
I cannot execute the same query SELECT ITM_NM FROM CM_ITM_ALL WHERE ITM_NUM='a800004' using my nodejs server. What is the problem here?
Here is the error message from the Oracle via my server:
[Error: ORA-00942: table or view does not exist] {
errorNum: 942,
offset: 19
}
Table does exist. Connection is not a problem as it is giving out the table does not exist error. Otherwise it would give out wrong credentials error.
What I tried
Appending SYS into the query: SELECT ITM_NM FROM SYS.CM_ITM_ALL WHERE ITM_NUM='a800004'
Quoting the table name inside the query to SELECT ITM_NM FROM 'SYS'.'CM_ITM_ALL' WHERE ITM_NUM='a800004'
Dev environment
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0
NodeJS v16.13.0
ExpressJs 4.17.2
node-oracledb 5.3.0
Oracle SQL Developer 21.4.2

Cannot update identity column 'scope_local_id'

Update I revised my approach to use 1 sync scope per database schema. This eliminated the problem for all schemas except dbo. Then I split dbo into several sync scopes, which seems to have eliminated the problem in dbo. It is not clear to me why having a large number of tables in a single sync scope would lead to this particular error message.
I am using Microsoft Sync Framework 2.1 to set up synchronization between two databases. I am provisioning the Azure database using the following code:
// Set up destination server for sync
var destinationScopeProvisioning = new SqlSyncScopeProvisioning(DestinationConnection, scope);
if (!destinationScopeProvisioning.ScopeExists(ScopeName))
{
destinationScopeProvisioning.Apply();
}
This code throws an exception after running for several minutes:
An unhandled exception of type 'Microsoft.Synchronization.Data.DbPartiallyProvisionedException' occurred in Microsoft.Synchronization.Data.SqlServer.dll
Additional information: Cannot update identity column 'scope_local_id'.
Ordinarily, this kind of error is caused by a scope that already exists, so I've tried the following three methods (several times) to clean up the scopes and start again:
Deprovisioning the scope by invoking deprovisioning.DeprovisionScope(ScopeName)
Deprovisioning the store by invokeing deprovisioning.DeprovisionStore()
Dropping and recreating the database.
Unfortunately, none of those worked.

Clustered Index Entity Framework

When I Create the azure database with Entity Framework model first pattern, then the creation works. But when I want to save the database I get the following error:
"Tables without a clustered index are not supported in this version of SQL Server. Please create a clustered index and try again."
I updated entity Framework to Version 6.1.2. But still get the same eroor. Do you have any idea?

SQL Azure unexpected database deletion/recreation

I've been scratching my head on this for hours, but can't seem to figure out what's wrong.
Here's our project basic setup:
MVC 3.0 Project with ASP.NET Membership
Entity Framework 4.3, Code First approach
Local environment: local SQL Server with 2 MDF database files attached (aspnet.mdf + entities.mdf)
Server environment: Windows Azure + 2 SQL Azure databases (aspnet and entities)
Here's what we did:
Created local and remote databases, modified web.config to use SQLEXPRESS connection strings in debug mode and SQL Azure connection strings in release mode
Created a SampleData class extending DropCreateDatabaseAlways<Entities> with a Seed method to seed data.
Used System.Data.Entity.Database.SetInitializer(new Models.SampleData()); in Application_Start to seed data to our databases.
Ran app locally - tables were created and seeded, all OK.
Deployed, ran remote app - tables were created and seeded, all OK.
Added pre-processor directives to stop destroying the Entity database at each application start on our remote Azure environment:
#if DEBUG
System.Data.Entity.Database.SetInitializer(new Models.SampleData());
#else
System.Data.Entity.Database.SetInitializer<Entities>(null);
#endif
Here's where it got ugly
We enabled Migrations using NuGet, with AutomaticMigrationsEnabled = true;
Everything was running smooth and nice. We left it cooking for a couple days
Today, we noticed an unknown bug on the Azure environment:
we have several classes deriving from a superclass SuperClass
the corresponding Entity table stores all of these objects in the same SuperClass table, using a discriminator to know which column to feed from when loading the various classes
While the loading went just fine before today, it doesn't anymore. We get the following error message:
The 'Foo' property on 'SubClass1' could not be set to a 'null' value. You must set this property to a non-null value of type 'Int32'.
After a quick check, our SuperClass table has columns Foo and Foo1. Logical enough, since SuperClass has 2 subclasses SubClass1 and SubClass2, each with a Foo property. In our case, Foo is NULL but Foo1 has an int32 value. So the problem is not with the database - rather, it would seem that the link between our Model and Database has been lost. The discriminator logic was corrupted.
Trying to find indications on what could've gone wrong, we noticed several things:
Even though we never performed any migration on the SQL Azure Entity database, the database now has a _MigrationHistory table
The _MigrationHistory table has one record:
MigrationID: 201204102350574_InitialCreate
CreatedOn: 4/10/2012 11:50:57 PM
Model: <Binary data>
ProductVersion: 4.3.1
Looking at other tables, most of them were emptied when this migration happened. Only the tables that were initially seeded with SampleData remained untouched.
Checking in with the SQL Azure Management portal, our Entity database shows the following creation date: 4/10/2012 23:50:55.
Here is our understanding
For some reason, SQL Azure deleted and recreated our database
The _MigrationHistory table was created in the process, registering a starting point to test the model against for future migrations
Here are our Questions
Who / What triggered the database deletion / recreation?
How could EF re-seed our sample data since Application_Start has System.Data.Entity.Database.SetInitializer<Entities>(null);?
EDIT: Looking at what could've gone wrong, we noticed one thing we didn't respect in this SQL Azure tutorial: we didn't remove PersistSecurityInfo from our SQL Azure Entity database connection string after the database was created. Can't see why on Earth it could have caused the problem, but still worth mentioning...
Nevermind, found the cause of our problem. In case anybody wonders: we hadn't made any Azure deployment since the addition of the pre-processor directives. MS must have restarted the machine our VM resided on, and the new VM recreated the database using see data.
Lesson learned: always do frequent Azure deployments.

Error exporting SQL Server 2008 R2 database to Excel 2007

I was trying to export SQL Server 2008 R2 database to Excel 2007. I used the export wizard in SQL Server Management Studio. I used a query to export some columns of a table. I checked the query (saw the result of parsing it) and it was good. But on the REVIEW DATA TYPE MAPPING it showed error notifications. It was converting varchar to LongText. But when I executed the package it failed to run showing fail status for steps like prepare for execute,pre-execute,execute . the error message is like this
Error 0xc002f210: Preparation SQL Task 1: Executing the query "CREATE TABLE Query (
id Long, sku LongText..." failed with the following error: "Unexpected error from external database driver (1309).". Possible failure
reasons: Problems with the query, "ResultSet" property not set
correctly, parameters not set correctly, or connection not established
correctly.
(SQL Server Import and Export Wizard)
Someone has a clue whats wrong?
Not an ideal answer (I have no idea what the cause of this problem is) but I was able to bypass this error by saving the excel sheet to 97-03 format and extracting to that.

Resources