我正在为我们的数据库团队创建一个供外部使用的迁移包,以执行实体框架迁移。该包由一个实体框架项目(包括我们所有的迁移)和来自Entity Framework6.4 NuGet包的ef6.dll迁移工具组成,该包位于ef64package/tools/netcoreapp3.0/any。
迁移包还包含一个以某种方式执行ef6.dll的简单PowerShell脚本,如下所示:
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 (在以前的实体框架版本中)和.NET框架的ef6.exe,这个捆绑包工作得很好。但在切换到.NET核心和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在这个过程中搞乱了。我有一种感觉,这是由于缺少配置或依赖项造成的副作用。但到目前为止,我还不知道如何修复这个错误。
对如何修复这个错误有什么想法吗?此外,我们也非常欢迎创建独立迁移包的其他方法。
提前感谢!
发布于 2020-06-02 20:24:31
这就是我是如何让它工作的:
dotnet exec --depsfile MyApp.Core.EF.json --runtimeconfig MyApp.Core.EF.runtimeconfig.json "....\packages\entityframework\6.4.0\tools\netcoreapp3.0\any\ef6.dll" database update --assembly MyApp.Core.EF.dll --connection-string "xxx" --verbose另请参阅此Github issue或Entity Framework Migration Azure DevOps Release Pipeline。
发布于 2021-12-08 15:05:52
Entity Framework (6.4.4) + Net Core 3.x的迁移需要一个可执行应用程序作为入口点。(“旧”迁移很好,只有包含数据库上下文的dll。)
你只需要添加一个可执行的应用程序(或者创建一个虚拟的命令行应用程序),并在命令中指定它。这是不明显的,因为所有示例都假设您只有一个项目和单个dll,并且不警告这一点。
如果你检查ef6.dll的语法,你会看到主应用程序dll的参数...不存在。
但是,可以通过使用未记录的dotnet exec语法来解决此问题。
dotnet exec --depsfile Your.App.Project.deps.json --runtimeconfig Your.App.Project.runtimeconfig.json .\ef6.dll" database update --verbose --assembly Your.Database.Project.dll --connection-string "<standard sql string>"
请注意,.deps文件和.runtimeconfig来自您的可执行应用程序,并且该应用程序的所有.dll文件必须存在于该文件夹中(工具将对此进行检查)。其余参数是ef6.dll的标准参数-包含数据库上下文、连接字符串、可选提供程序类型的.dll文件的名称。
一些注意事项:
Server=.\SQLEXPRESS而不是Server=.\\SQLEXPRESS时,得到另一个晦涩的错误并不有趣
这篇文章的作者现在正式讨厌ef6.dll。
https://stackoverflow.com/questions/61012668
复制相似问题