问题
I'm using Code first Entity Framework 5 in my MVC project. Today I was making some changes to my domain model and updated the rest of the application to work with these new changes. Naturally, when changes to the domain model is made, you'll need to update the database. We're using code migrations to do that (manual migration, that is). However when I tried to add a new migration through the package console, i'm getting the following exception:
Exception has been thrown by the target of an invocation.
I've tried adding the startup project to the command, which didn't work either. All of my projects build so no compiler errors either.
EDIT: Also, this happens no matter what I do: update-database or update-database with a target migration. All seem to give me the same exception.
This is the callstack:
PM> add-migration MyMigrationName
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Data.Entity.Migrations.Infrastructure.AutomaticMigrationsDisabledException: Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.
at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
at System.Data.Entity.MigrateDatabaseToLatestVersion`2.InitializeDatabase(TContext context)
at System.Data.Entity.Database.<>c__DisplayClass2`1.<SetInitializerInternal>b__0(DbContext c)
at System.Data.Entity.Internal.InternalContext.<>c__DisplayClass8.<PerformDatabaseInitialization>b__6()
at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
at System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c)
at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
at System.Data.Entity.Internal.InternalContext.ForceOSpaceLoadingForKnownEntityTypes()
at System.Data.Entity.DbContext.System.Data.Entity.Infrastructure.IObjectContextAdapter.get_ObjectContext()
at EntityFramework.Audit.AuditLogger..ctor(DbContext dbContext, AuditConfiguration configuration)
at EntityFramework.Extensions.AuditExtensions.BeginAudit(DbContext dbContext, AuditConfiguration configuration)
at Project.DataAccessLogic.MyContext..ctor() in c:..\MyContext.cs:line 125
--- End of inner exception stack trace ---
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at System.Data.Entity.Infrastructure.DbContextInfo.<CreateActivator>b__0()
at System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo)
at System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType)
at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext)
at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator()
at System.Data.Entity.Migrations.Design.ToolingFacade.GetPendingMigrationsRunner.RunCore()
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
Exception has been thrown by the target of an invocation.
I'm not sure where I should start to look.
回答1:
I get this error when I don't set up my connection strings properly.
Make sure the following are correct in your Web.config:
- Data Source
- Initial Catalog
- User Id
- Password
回答2:
Had the same issue with Table-First update/generate from model. The solution was to replace the dot in the app.config connection string with the actual machine name of the SQL Server instance.
回答3:
I got the exception because of branches in source code.
- Switched to branch feature.
- Created a code migration.
- Applied changes to a database.
- Switched to default.
Code migration is absent in project, but the database contains info about it.
回答4:
I had this issue even though the __Migrations
table and the /Migrations cs files were in sync. No migration had been run recently, either. I "fixed" it with Add-Migration "Anything"
. Then I just deleted the newly scaffolded empty CS and designer files.
回答5:
A bit late, but perhaps this is helpful to someone.
Several things you could check:
Make sure you are connecting to the right database: make sure you selected the right project in the console
Default project
drop-down list, or explicitly use the parameter-StartUpProjectName
. This project must contain the right connection string in its settings. You can also explicitly set the-ConnectionString
parameter.If your solution has a startup project the console will use its connection string even if you stated a different project in the console
Default project
drop-down list or in the parameter-StartUpProjectName
. Make sure you have the right startup project in the solution. Another way is having selectedMultiple startup projects
in you solution configuration options. In that case the console will accept the values you provided instead of looking for the connection string in a solution startup project.Perhaps your current database is not synchronised with your current last migration (the one previous to the new one you are trying to generate). Try running
Get-Migrations
to see which is the current migration applied to your database. If it is not the latest one, runUpdate-Database
using the parameters-SourceMigration
and-TargetMigration
to update it to the latest migration.
来源:https://stackoverflow.com/questions/18440304/entity-framework-code-migrations-exception-has-been-thrown-by-the-target-of-an