实体框架代码迁移:调用的目标引发了异常 [英] Entity Framework Code Migrations: Exception has been thrown by the target of an invocation

查看:321
本文介绍了实体框架代码迁移:调用的目标引发了异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的MVC项目中使用的是Code First Entity Framework 5.今天,我对我的域模型进行了一些更改,并更新了应用程序的其余部分以适应这些新更改.自然,在对域模型进行更改时,您将需要更新数据库.我们正在使用代码迁移来做到这一点(即手动迁移).但是,当我尝试通过程序包控制台添加新迁移时,出现以下异常:

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.

而且,无论我做什么,都会发生这种情况:具有目标迁移的update-database或update-database.所有人似乎都给我一个例外.

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.

这是调用栈:

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.

推荐答案

当我没有正确设置连接字符串时,会出现此错误.

I get this error when I don't set up my connection strings properly.

请确保您的Web.config中的以下内容正确无误:

Make sure the following are correct in your Web.config:

  1. 数据源
  2. 初始目录
  3. 用户ID
  4. 密码

这篇关于实体框架代码迁移:调用的目标引发了异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆