EntityFramework 6.0 CreateDatabaseIfNotExists Code first to create database

前端 未结 2 1674
广开言路
广开言路 2020-12-29 12:13

What am I doing wrong. I have got a user DbContext setup and working when I originally created the Code-First with powershell it all worked fine.

I implemented Data

相关标签:
2条回答
  • 2020-12-29 12:45

    Edit: With your new error message the problem comes from you having migrations enabled and already ran a migration (probably the first creation of the database) and since you dropped the DB the migration history has been lost. If your not using Automatic migrations you can not go in and make changes to the database your self and expect code-first to know about it. Try disabling migrations and re-enabling them to see if that clears out the migration history.

    You will need to make a call into the DB either as a read or insert of data for the DB to initially be created. The code you use only tells EF how to deal with a database if one does not exist when it tries to find it. For me I use the method outlined at http://www.codeproject.com/Tips/411288/Ensure-Your-Code-First-DB-is-Always-Initialized

    0 讨论(0)
  • 2020-12-29 12:47

    Finally figured the solutions, not sure why or what. Changed my Database initializer to MigrateDatabaseToLatestVersion instead of CreateDatabaseIfNotExists worked.

    Database.SetInitializer<UserDbContext>(new MigrateDatabaseToLatestVersion<UserDbContext, Configuration>());
    
    0 讨论(0)
提交回复
热议问题