【发布时间】:2020-04-03 13:11:04
【问题描述】:
我正在创建一个迁移包,供我们的数据库团队外部使用,以执行实体框架迁移。该捆绑包由实体框架项目(包括我们所有的迁移)和来自实体框架 6.4 NuGet 包的ef6.dll 迁移工具组成,该工具位于ef64package/tools/netcoreapp3.0/any。
迁移包还包含一个简单的 PowerShell 脚本,它以某种方式执行 ef6.dll,如下所示:
dotnet .\ef6.dll database update --verbose --assembly MyApp.Core.EF.dll --connection-string "Data Source=localhost;Initial Catalog=MyApp;Trusted_Connection=True;" --connection-provider "System.Data.SqlClient"
使用 migrate.exe(在早期版本的 Entity Framework 中)和用于 .NET Framework 的 ef6.exe 这个捆绑包工作得很好。但是在切换到.NET Core和ef6.dll之后执行会出现如下错误:
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Data.Entity.Internal.LazyInternalConnection.CreateConnectionFromProviderName(String providerInvariantName)
at System.Data.Entity.Internal.LazyInternalConnection.InitializeFromConnectionStringSetting(ConnectionStringSettings appConfigConnection)
at System.Data.Entity.Internal.LazyInternalConnection.Initialize()
at System.Data.Entity.Internal.LazyInternalConnection.get_Connection()
at System.Data.Entity.Internal.LazyInternalContext.get_Connection()
at System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo, Func`1 resolver)
at System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbConnectionInfo connectionInfo)
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.Infrastructure.Design.Executor.CreateMigrator(DbMigrationsConfiguration configuration)
at System.Data.Entity.Infrastructure.Design.Executor.UpdateInternal(String targetMigration, Boolean force, DbConnectionInfo connectionInfo, String migrationsConfigurationName)
at System.Data.Entity.Infrastructure.Design.Executor.Update.<>c__DisplayClass0_0.<.ctor>b__0()
at System.Data.Entity.Infrastructure.Design.Executor.OperationBase.Execute(Action action)
Object reference not set to an instance of an object.
一个非常一般的错误,但根据 EF 源代码 (https://github.com/dotnet/ef6/blob/02104044fae2aca5d3a1f50f55f4af940a3c64a8/src/EntityFramework/Internal/LazyInternalConnection.cs),我看起来好像缺少 DbProviderFactory 注册或 providerInvariantName 在此过程中搞砸了。我有一种感觉,这是由于缺少配置或依赖关系造成的副作用。但到目前为止我无法弄清楚如何解决这个错误。
关于如何修复此错误的任何想法? 其他创建独立迁移包的方法也非常受欢迎。
提前致谢!
【问题讨论】:
标签: entity-framework .net-core