5

I have enabled automatic migrations. Then, I deleted my whole db. Next, i executed Update-database from command console, and it recreated my db. Then, I started my application only to see this error:

Model compatibility cannot be checked because the database does not contain model metadata. Model compatibility can only be checked for databases created using Code First or Code First Migrations.

So what exactly is that metadata, and how can I point entity framework to it?

PS. My database contains table named MigrationsHistory.

jbutler483
  • 24,074
  • 9
  • 92
  • 145
ojek
  • 9,680
  • 21
  • 71
  • 110

5 Answers5

6

Here is a detailed description of the possible ways to resolve this which I wrote a while ago...
(not exactly what you're experiencing, hence not a duplicate per se, but with different scenarios in mind)

https://stackoverflow.com/a/10255051/417747

To summarize...

What works for me is to use Update-Database -Script

That creates a script with a 'migration difference', which you can manually apply as an SQL script on the target server database (and you should get the right migration table rows inserted etc.).

If that still doesn't work - you can still do two things...

a) remove the migration table (target - under system tables) - as per http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-automatic-migrations-walkthrough.aspx comments in there - that should fail back to previous behavior and if you're certain that your Db-s are the same - it's just going to 'trust you',

b) as a last resort I used - make a Update-Database -Script of the full schema (e.g. by initializing an empty db which should force a 'full script'), find the INSERT INTO [__MigrationHistory] records, just run those, insert them into the database, and make sure that your databases - and code match,

that should make things run in sync again.

if it helps

Community
  • 1
  • 1
NSGaga-mostly-inactive
  • 14,052
  • 3
  • 41
  • 51
  • not found https://blogs.msdn.microsoft.com/b/adonet/archive/2012/02/09/ef-4-3-automatic-migrations-walkthrough.aspx – Kiquenet Jun 03 '17 at 12:20
  • @Kiquenet there isn't much in there, let me know what you need - it's the `__MigrationHistory` table that you need to remove - and as far as I remember there isn't anything else related to it (just one table, under system tables). The comment was along these lines, if the EF doesn't find that table it just falls back to 'trusting' you, i.e. that your db schema and code is in sync. So if you know those are in sync you can always remove that table (or I suggest do under (b)). And I had rephrased that comment under the (a) above. So nothing lost :) – NSGaga-mostly-inactive Jun 09 '17 at 09:32
  • page can be found here [archive](http://web.archive.org/web/20160718142810/http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-automatic-migrations-walkthrough.aspx) but indeed comments section is lost (where there was some discussion - boiling down to what I've said above) – NSGaga-mostly-inactive Jun 09 '17 at 09:35
1

Detach your local Database say example 'database1.mdf' from visual studio 'server explorer' and then open SQL server management studio right click on Databases > Attach and then browse the same 'database1.mdf' file .If you doesn't have access then copy&past both the mdf and ldf files into c drive and do attach.

Then open new query window on sql server then do copy your identity tables as like below query.

*'select * into [__MigrationHistory] from Database1.dbo.__MigrationHistory '*

-1

Add this to your context:

protected override void OnModelCreating(ModelBuilder modelBuilder) 
{
    modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
}
Nikos
  • 7,295
  • 7
  • 52
  • 88
-1

I use Entity Framework 6 , SQL 2008 R2 , VS 2013.
To solve this problem only use the following procedure :

1) delete existing db ( existing database that created with EF model{code first})
2) Run APP again.

Fore example query code (in Layout):
this code create db if my model is change and search username in user table.

<body>
    @{
        // Delete && Create ...
        Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DBContext>());

        var db = new DBContext();
        var SearchingUser = db.Users.Where(c => c.UserName == "qwertyui");
        if (SearchingUser.Count() == 0) {
            var User = new Users { UserName = "qwertyui",Password = "12345678" };
            db.Users.Add(User);
            db.SaveChanges();
        }

    }


    @RenderSection("scripts", required: false)
    @RenderBody()
</body>
Amin Ghaderi
  • 1,006
  • 13
  • 22
-1
  1. Package Manager Console > Enable-Migrations -EnableAutomaticMigrations
  2. Configure Migrations/Configuration.cs
  3. Package Manager Console > Update-Database