VS 2017,Microsoft.Build.Framework上的EF 6.13 add-migration抛出异常

时间:2018-10-02 13:00:19

标签: c# visual-studio entity-framework asp.net-mvc-5 ef-migrations

我输入添加迁移时收到此错误。 ApplicationDbContext存在于另一个项目中,而迁移文件存在于Web应用程序项目中。

如果我没记错的话,它曾经工作过,但是现在停止工作了。 我尝试重新启动Visual Studio,以管理员身份运行它。重新安装EF 6.11、6.13和6.20。

Visual Studio版本15.8.5,构建工具15.1

迁移存在于启动项目中,并且程序包管理器也已设置在启动项目中。

我什至尝试修复Visual Studio,并且在尝试卸载然后再安装后也无济于事。

我尝试了无法编辑devenv.exe.config的解决方案 https://github.com/aspnet/EntityFramework6/issues/382

add-migration AddedUserFax
    System.TypeInitializationException: The type initializer for 'Company.SoftwareX.Contexts.ApplicationDbContext' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Build.Framework, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
       at Company.SoftwareX.Database.Contexts.ApplicationDbContext..cctor()
       --- End of inner exception stack trace ---
       at Company.SoftwareX.Database.Contexts.ApplicationDbContext..ctor() in C:\Projects\Visual Studio\Project\Company.SoftwareX.Database\ApplicationDbContext.cs:line 110
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Data.Entity.Infrastructure.DbContextInfo.CreateInstance()
       at System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo, Func`1 resolver)
       at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase)
       at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
       at System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration migrationsConfiguration)
       at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Run()
       at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
       at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
       at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
       at System.Data.Entity.Migrations.Design.ToolingFacade.Scaffold(String migrationName, String language, String rootNamespace, Boolean ignoreChanges)
       at System.Data.Entity.Migrations.AddMigrationCommand.Execute(String name, Boolean force, Boolean ignoreChanges)
       at System.Data.Entity.Migrations.AddMigrationCommand.<>c__DisplayClass2.<.ctor>b__0()
       at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
    The type initializer for 'Company.SoftwareX.Database.Contexts.ApplicationDbContext' threw an exception.

1 个答案:

答案 0 :(得分:1)

此问题的答案是在DbContext中没有静态构造函数,或者在构造函数中没有其他静态方法,而在创建新实例之前,静态方法必须运行。

主要线索在于堆栈跟踪, System.TypeInitializationException:...的类型初始值设定项。这表明静态属性/构造函数(按合同规定,必须在任何实例运行之前运行)进行更新)抛出错误。 -花费者