Reputation: 870
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__DisplayClass21.<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.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.
Upvotes: 5
Views: 32455
Reputation: 307
Update 'Microsoft.EntityFrameworkCore.SqlServer' and 'Microsoft.EntityFrameworkCore.Tools' to the latest of the framework version (dotnet5, dotnet6 etc) and run the migrations again. In my case, then encountered 'More than one DbContext was found. Specify which one to use. Use the '-Context' parameter for PowerShell commands and the '--context' parameter for dotnet commands.', which i then used Get-DbContext to get the context classes involved after which i then specificied the target conext class like 'add-migration InitialIdentityServerMigration -Context PersistedGrantDbContext'
Upvotes: 0
Reputation: 1
The exception is because the following need to be the same version.
So whatever version you are using, just make sure they match and you will be good to go.
Upvotes: 0
Reputation: 1
Using below latest stable version resolves my problem.
Microsoft.EntityFrameworkCore.SqlServer (5.0.0)
Microsoft.EntityFrameworkCore.Tools (5.0.0)
Upvotes: 0
Reputation: 11
I fixed this issue by Updating Entity Framework NuGet Packages to version 3.1.3 namely:
Upvotes: 1
Reputation: 502
I fixed this problem by adding --context and specify the db context class name.
Upvotes: 0
Reputation: 2226
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 selected Multiple 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, run Update-Database
using the parameters -SourceMigration
and -TargetMigration
to update it to the latest migration.
Upvotes: 1
Reputation: 31
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.
Upvotes: 1
Reputation: 11785
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.
Upvotes: 0
Reputation: 31
I get this error when I don't set up my connection strings properly.
Make sure the following are correct in your Web.config:
Upvotes: 3
Reputation: 5027
I got the exception because of branches in source code.
Code migration is absent in project, but the database contains info about it.
Upvotes: 0