Unable to update database to match the current mod

2020-03-24 03:42发布

I, for the life of me, can't get rid of this error message. I have tried almost everything that I can.

MyDBContext.cs

        public MyDBContext() : base("ConnStr_Dev")
    {

    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDBContext, DbMigrationsConfiguration<MyDBContext>>());
        //Database.SetInitializer<MyDBContext>(null);
        base.OnModelCreating(modelBuilder);
    }

GenericRepository.cs

        public void Insert(T obj)
    {
        table.Add(obj);      //this is where it throws the error, inserting first time.
    }

Tried all of these in different combinations

Enable-Migrations -EnableAutomaticMigrations -Force

Add-migration Initial -IgnoreChanges

Update-Database

I have tried deleting the Migrations table, deleting the entire database, everything.

Any Migrations experts please?

Edit: the DAL contains the GenericRepository and the context class.

5条回答
欢心
2楼-- · 2020-03-24 04:21

This error is weird enough in my case, I forgot to uncomment the CONNECTIONSTRING setting, means having a proper connectionstring resolved this error. Hope it helps!

查看更多
太酷不给撩
3楼-- · 2020-03-24 04:24
  1. Delete your Migrations folder from your solution
  2. Delete the dbo.__MigrationHistory table from your database
  3. Open Package Manager Console and Enable-Migrations
  4. Add your initial migration Add-Migration Initial
  5. Update-Database
  6. Done
查看更多
淡お忘
4楼-- · 2020-03-24 04:31

I fought with this error too. I added the migration and then made what I thought was a trivial change.

This property: IMigrationMetadata.Target in your migration isn't random. It's computed based on the state of the model at the time the migration was completed. (That may be an oversimplification.)

So if you make additional changes, it looks as there are additional pending changes that require yet another migration.

In my case, because the changes hadn't been committed to source control, the fix was to delete and re-add the migration. It took a while to figure out the cause because the error message is unclear unless you already know what's causing it. But the lesson I learned (being relatively new to EF) is that changes to the models require either a new migration or re-doing the migration that I'm working on.

查看更多
Anthone
5楼-- · 2020-03-24 04:34

In my case the error was resolved by adding a DatabaseInitializerForType... appSetting.

<appSettings>
<add key="DatabaseInitializerForType EasyEntity.EasyContext, EasyEntity"  value="EasyEntity.EasyInitializer, EasyEntity" />
查看更多
虎瘦雄心在
6楼-- · 2020-03-24 04:38

You simply made changes to one or more of your entity classes. Whether you added a new property, changed the data type of that particular property or you simply added a new entity class, in all of those cases you indeed need to add a new migration.

Seeing that you've already tried that, make sure that when you execute the Add-Migration command in Package Manager Console that you've selected the project that contains your DBContext class, the Configuration class and Migrations folder. Furthermore, you are passing a specific connection string to the base class (DbContext) of the MyDBContext class. Make sure you are also upgrading and updating the correct database.

Know that you can perform an Add-Migration command and an Update command to a specific database:

Example snippets:

Add-Migration AddProperty1 -ConnectionString "Data Source=.\SQLEXPRESS;Initial Catalog=MyDatabaseCatablog;Connect Timeout=30;User ID=MyUser;Password=mypassword12345" -ConnectionProviderName "System.Data.SqlClient" -Verbose

and

Update-Database -ConnectionString "Data Source=.\SQLEXPRESS;Initial Catalog=MyDatabaseCatablog;Connect Timeout=30;User ID=MyUser;Password=mypassword12345" -ConnectionProviderName "System.Data.SqlClient" -Verbose

Hope this helps.

查看更多
登录 后发表回答