29

I am using Entity Framework 5 and doing a simple query to get a few users from a table.

The SQL database is already created so I use my entities as a way to map what I have.

When I run my query I detect that other queries were executed and 2 of them with errors:

QUERY 1

ERROR: Invalid object name 'dbo.__MigrationHistory'.

SELECT
[GroupBy1].[A1] AS [C1]
FROM ( 
    SELECT
        COUNT(1) AS [A1]
    FROM [dbo].[__MigrationHistory] AS [Extent1]
) AS [GroupBy1]

QUERY 2

ERROR: Invalid object name 'dbo.EdmMetadata'.

SELECT TOP (1)
   [Extent1].[Id] AS [Id],
   [Extent1].[ModelHash] AS [ModelHash]
FROM [dbo].[EdmMetadata] AS [Extent1]
ORDER BY [Extent1].[Id] DESC

Why is that?

I do not have dbo.EdmMetadata and dbo.__MigrationHistory tables in my database as the database already existed.

How to solve this?

Mikael Östberg
  • 16,982
  • 6
  • 61
  • 79
Miguel Moura
  • 36,732
  • 85
  • 259
  • 481

4 Answers4

60

Since the database is already there you will not have dbo.EdmMetadata and dbo.__MigrationHistory which codefirst is expecting. And to resolve this you can try to set the SetInitializer to null.

 static NameOfYourContext()
 {
   Database.SetInitializer<NameOfYourContext>(null);        
 }      

You can see this in the comments section of the this post by Scott Gu

Mitul
  • 9,734
  • 4
  • 43
  • 60
  • Works great. Note that your constructor may not be `static`, and the above code doesn't exist with a non-null argument -- you'll have to add the whole line. – Alex Jul 05 '19 at 12:04
1

There are 3 steps you need to follow:

1- Enable migrations in package manager if you haven't done yet:

Enable-Migrations

2- Add a migration and make sure to use the -IgnoreChanges switch as you already have an existing database:

Add-Migration InitialModel -IgnoreChanges

3- Update the database. This will automatically create __MigrationHistory table for you.

Update-Database
Mosh
  • 5,944
  • 4
  • 39
  • 44
0

I disabled my exception setting. and it overlooked this exception and went on to create these tables for me automatically

Blue Clouds
  • 7,295
  • 4
  • 71
  • 112
  • 1
    Pls provide code sample on how you "disabled exception setting". Otherwise it's not a useful answer. – OzBob Dec 20 '18 at 07:19
0

Environment: ADO.net physical data model, import from database code first, Entity Framework Ver.6.0, Visual Studio 2022, C#

Story: I have been plagued by the dbo.EdmMetadata & dbo.__MigrationHistory table missing exception(not visible, but shown in debug terminal, and will have trouble if SQL accessing account is without create table permission), search the net but all I found is the "Database.SetInitializer(null)" solution, which doesn't really fit in the code in its original form. no matter how I tried to modify it.

Result: After hours of fumbling, I actually saved by ChatGPT (although gave me many unsuccessful suggestions first), the following is the solution, should applied to Entity Framework Ver.6 and above:

  1. Create the following class(NameOfYourContext is the class name of your database model)
public class MyDbConfiguration : DbConfiguration
{
    public MyDbConfiguration()
    {
        SetDatabaseInitializer<NameOfYourContext>(null);
    }
}

2.Put the following code in your program entry point(for Winform application, program.cs -> Main function)

DbConfiguration.SetConfiguration(new MyDbConfiguration());

Hope this answer will be helpful.