Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid object name 'dbo.EdmMetadata' and 'dbo.__MigrationHistory'

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?

like image 248
Miguel Moura Avatar asked Oct 15 '13 11:10

Miguel Moura


2 Answers

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

like image 196
Mitul Avatar answered Nov 09 '22 13:11

Mitul


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
like image 1
Mosh Avatar answered Nov 09 '22 12:11

Mosh