gpt4 book ai didi

c# - Entity Framework 代码迁移 : Exception has been thrown by the target of an invocation

转载 作者:行者123 更新时间:2023-11-30 22:14:36 25 4
gpt4 key购买 nike

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

Exception has been thrown by the target of an invocation.

我试过将启动项目添加到命令中,但也没有用。我构建的所有项目都没有编译器错误。

编辑:此外,无论我做什么,都会发生这种情况:更新数据库或更新数据库与目标迁移。似乎所有人都给了我同样的异常(exception)。

这是调用栈:

PM> add-migration MyMigrationNameSystem.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(IEnumerable1 pendingMigrations, String targetMigrationId, String lastMigrationId) at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) at System.Data.Entity.MigrateDatabaseToLatestVersion2.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.RetryAction1.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.

我不确定应该从哪里开始看。

最佳答案

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

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

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

关于c# - Entity Framework 代码迁移 : Exception has been thrown by the target of an invocation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18440304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com