dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.72k stars 3.17k forks source link

Can I make migration bundle to behave as if IncludeAllContentForSelfExtract is set? #33112

Open TomaszWegrzyn opened 8 months ago

TomaszWegrzyn commented 8 months ago

Description

I am generating migration bundle and getting exception(System.NotSupportedException: CodeBase is not supported on assemblies loaded from a single-file bundle) when executing the bundle. It seems to me that the issue is related to one of libraries that I need, specifically SAP Hana EF Core library. It is trying to load Native DLL and failing. I am under the impression that ef migrations bundle is the same/similar to single file publish, so I would hope I can manipulate IncludeAllContentForSelfExtract setting as well, because I think that should solve the problem I am describing.

When publishing my application as single file, setting <IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract> in my .csproj indeed prevents System.NotSupportedException, which leads to me believe I would need the same kind of setting for migration bundles. Unfortunately it seems to not do anything when it comes to migration bundle.

If I am wrong please correct me - maybe I am confused about something.

Details

I am generating bundle with below command

dotnet ef migrations bundle --force --self-contained

Then running the bundle produces following exception:

efbundle.exe --verbose

Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly 'WebApplication1'...
Finding Microsoft.Extensions.Hosting service provider...
Using environment 'Development'.
Using application service provider from Microsoft.Extensions.Hosting.
Finding DbContext classes in the project...
Found DbContext 'HanaDbContext'.
Using context 'HanaDbContext'.
Finding design-time services referenced by assembly 'WebApplication1'...
Finding design-time services referenced by assembly 'WebApplication1'...
No referenced design-time services were found.
Finding design-time services for provider 'Sap.EntityFrameworkCore.Hana.v7.0'...
Using design-time services from provider 'Sap.EntityFrameworkCore.Hana.v7.0'.
Finding IDesignTimeServices implementations in assembly 'WebApplication1'...
No design-time services were found.
Creating DbConnection.
'HanaDbContext' disposed.
System.TypeInitializationException: The type initializer for 'Sap.Data.Hana.HanaConnection' threw an exception.
 ---> System.NotSupportedException: CodeBase is not supported on assemblies loaded from a single-file bundle.
   at System.Reflection.RuntimeAssembly.get_CodeBase()
   at Sap.Data.Hana.HanaUnmanagedDll.SearchNativeDlls()
   at Sap.Data.Hana.HanaUnmanagedDll..ctor()
   at Sap.Data.Hana.HanaUnmanagedDll.get_Instance()
   at Sap.Data.Hana.HanaConnection..cctor()
   --- End of inner exception stack trace ---
   at Sap.Data.Hana.HanaConnection..ctor(String connectionString)
   at Sap.EntityFrameworkCore.Hana.Storage.Internal.HanaRelationalConnection.CreateDbConnection()
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.get_DbConnection()
   at Microsoft.EntityFrameworkCore.Diagnostics.RelationalLoggerExtensions.MigrateUsingConnection(IDiagnosticsLogger`1 diagnostics, IMigrator migrator, IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsBundle.ExecuteInternal(String[] args)
   at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsBundle.<>c__DisplayClass6_0.<Configure>b__0(String[] args)
   at Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(String[] args)
   at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsBundle.Execute(String context, Assembly assembly, Assembly startupAssembly, String[] args)
The type initializer for 'Sap.Data.Hana.HanaConnection' threw an exception.

Note that SAP provides different binaries for Windows and Linux, but same problem happens on Linux(--target-runtime linux-x64) as well as Windows(I am including different dependencies conditionally based on OS)

I can run the application just fine on both OSes when running from Visual Studio or Docker compose. So I don't think OS compatibility has anything to do with it.

Unfortunately I can't show the source code due to closed source, but I hope my explanation is clear enough.

Provider and version information

EF Core version: 7.0.16 Database provider: Sap.EntityFrameworkCore.Hana.v7.0.dll (published via private nuget) Target framework: (NET 8.0) Operating systems: Windows 10/Linux

ajcvickers commented 7 months ago

/cc @maumar