question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invalid object name 'dbo.__MigrationHistory'

See original GitHub issue

When debugging in Visual Studio we are getting the following Exception pretty often:

"System.Data.SqlClient.SqlException" in System.Data.dll
Invalid object name 'dbo.__MigrationHistory'.

Entity Framework is right here we don’t use ‘dbo.__MigrationHistory’ and we dont use Code First Migrations. We are only using EF for Audit.

After a little investigation it seems that one can turn off the check for ‘dbo.__MigrationHistory’ by setting Database.SetInitializer<YourContext>(null); according to this StackOverflow [Link:] (https://stackoverflow.com/questions/20877039/invalid-object-name-dbo-migrationhistory-using-database-create-ef6-02-when)

is there a way to tell the Audit.Core.Configuration.DataProvider to use Database.SetInitializer<YourContext>(null); ? Or another way to stop the cause of the exception?

Heres the Code to create the audit scope

` private void SetAuditProvider(DirectDbDataAccess db)
        {
            Audit.Core.Configuration.DataProvider = new SqlDataProvider()
            {
                ConnectionString = db.ConnectionInfo.ConnectionString,
                Schema = "dbo",
                TableName = "Audit",
                IdColumnName = "EventId",
                JsonColumnName = "Data",
                LastUpdatedDateColumnName = "LastUpdatedDate",
                CustomColumns = new List<CustomColumn>()
                {
                    new CustomColumn("EventType", ev => ev.EventType)
                }
            };

            Audit.Core.Configuration.AddCustomAction(ActionType.OnScopeCreated, scope =>
            {
                if (!m_audit.isActive()) scope.Discard();
            });
        }`

heres the code to create the table:

CREATE TABLE [Audit]
                (
	                [EventId] BIGINT IDENTITY(1,1) NOT NULL,
	                [InsertedDate] DATETIME NOT NULL DEFAULT(GETUTCDATE()),
	                [LastUpdatedDate] DATETIME NULL,
	                [Data] NVARCHAR(MAX) NOT NULL,
                        [EventType] NVARCHAR(100) NOT NULL,
	                CONSTRAINT PK_Event PRIMARY KEY (EventId)
                )

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
thepirat000commented, Aug 12, 2019

ok I will release a version with the first option soon

0reactions
b4nakicommented, Aug 13, 2019

I updated this moring. works great. thank you for help and your efforts you put into Audit.net 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Invalid object name 'dbo.__MigrationHistory' using ...
It means that EF is not able to find the __Migration history table. also note ... I forgot to add the database name...
Read more >
[Solved]-Error: Invalid object name 'dbo.__MigrationHistory ...
Coding example for the question Error: Invalid object name 'dbo.__MigrationHistory'. with custom schema name-Entity Framework.
Read more >
Why Invalid Object Name 'dbo.__MigrationHistory'?
It creates __MigrationHistory on the default schema, but queries for it on the dbo schema. After a while I find another problem. Entity ......
Read more >
Entity-framework – Invalid object name 'dbo.EdmMetadata' ...
I am using Entity Framework 5 and doing a simple query to get a few users from a table. The SQL database is...
Read more >
getting this “Invalid object name 'dbo.*'” error with MVC4
Entity-framework – Invalid object name 'dbo.EdmMetadata' and 'dbo.__MigrationHistory'. Since the database is already there you will not have dbo.EdmMetadata and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found