dotnet / dotnet/efcore

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

Open
#33,112 2 comments 1 reaction 0 assignees View on GitHub
area-migrations area-tools customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

### 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](https://help.sap.com/docs/SAP_HANA_CLIENT/f1b440ded6144a54ada97ff95dac7adf/3e6ef454ffc94cda8fefb0acf5be007b.html). It is trying to load Native DLL and failing.
I am under the impression that `ef migrations bundle` is [the same/similar](https://github.com/dotnet/efcore/issues/19693#issuecomment-1201474149) 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 `true` 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.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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.