Unable to generate an explicit migration in entity

2019-01-21 18:13发布

I am adding a new migration but this message shows:

Unable to generate an explicit migration because the following explicit migrations are pending: [201203170856167_left]. Apply the pending explicit migrations before attempting to generate a new explicit migration.

Can any one help me?

20条回答
闹够了就滚
2楼-- · 2019-01-21 18:33

If you haven't used Update-Database you can just delete it. If you've run the update then roll it back using Update-Database -TargetMigration "NameOfPreviousMigration", then delete it.

Reference: http://elegantcode.com/2012/04/12/entity-framework-migrations-tips/

I copied this text directly from here: How do I undo the last Add-Migration command?

查看更多
我只想做你的唯一
3楼-- · 2019-01-21 18:35

It tells you that there is some unprocessed migration in your application and it requires running Update-Database before you can add another migration.

查看更多
啃猪蹄的小仙女
4楼-- · 2019-01-21 18:37

Tip: It's always good to use the -Script switch for migration commands if you're not sure. It also really helps understand what Update-Database actually does.

I run the following to update the database, then I get a script I can apply manually (or just run it again without the -Script tag).

For Update-Database I would run the following :

Update-Database -Script -ConfigurationTypeName Configuration_ASPNETIdentity -ConnectionStringName SQL_AzureLive

Where SQL_AzureLive is the named connection string in my config.

Then I can verify the SQL looks right, apply it and be done. As many others have said if the connection string is wrong or invalid you'll get this error.

查看更多
姐就是有狂的资本
5楼-- · 2019-01-21 18:38

1. Connection String / Connection Permissions

Check the connection string again.

Make sure the user you are connecting with still has permission to read from [__MigrationHistory] and has permission to edit the schema.

You can also try changing the connection string in the App or Web config file to use Integrated Security (Windows Auth) to run the add-migration command as yourself.

For example:

connectionString="data source=server;initial catalog=db;persist security info=True;Integrated Security=SSPI;" 

This connection string would go in the App.config file of the project where the DbContext is located.

2. StartUp Project

You can specify the StartUp project on the command line or you can right click the project with the DbContext, Configuration and Migrations folder and select Set as StartUp project. I'm serious, this can actually help.

enter image description here

查看更多
Deceive 欺骗
6楼-- · 2019-01-21 18:40

Had the same issue and was able to solve with some hints from above answers:

  • In package manager console check the default project (point to the project with the migration configuration
  • Ensure the startup-proj has a web.config with a valid connectionstring ( or
  • Ensure the project with migrations has an app.config / web.config with a valid connectionstring
  • Check permissions in DB (for the user configured in you connectionstring)

Use "update-database -verbose" in the package manager console to get more specific information where migrations tries to connect to. (Helped in my case to find out my startup proj was not set correctly...)

查看更多
forever°为你锁心
7楼-- · 2019-01-21 18:41

I had the same problem. Apparently entity framework generates this error when it's unable to connect to the database. So make sure that you're able to access it before searching for other problems.

查看更多
登录 后发表回答