ORMConfigurationException in AddDbProviderFactory with SqlClientFactory

Posts   
 
    
PeterVD
User
Posts: 15
Joined: 16-Jan-2007
# Posted on: 01-May-2022 20:01:16   

Hi,

I'm building a .NET 6 Blazor app (WebAssembly, Progressive Web App) with an Azure SQL Database. My LLBLGen version is 5.7.3, and the code is generated for LLBLGen Pro Runtime Framework, Adapter for .NET Standard 2.1.

My issue is related to this thread

I also get the error "The static field 'Instance' of type 'System.Data.SqlClient.SqlClientFactory' is null and therefore this DbProviderFactory can't be used." on this statement.

RuntimeConfiguration.ConfigureDQE<SQLServerDQEConfiguration>(
    c => c.SetTraceLevel(TraceLevel.Verbose)
        .AddDbProviderFactory(typeof(SqlClientFactory))
        .SetDefaultCompatibilityLevel(SqlServerCompatibilityLevel.SqlServer2012));

System.AggregateException: One or more errors occurred. (The static field 'Instance' of type 'System.Data.SqlClient.SqlClientFactory' is null and therefore this DbProviderFactory can't be used.) ---> SD.LLBLGen.Pro.ORMSupportClasses.ORMConfigurationException: The static field 'Instance' of type 'System.Data.SqlClient.SqlClientFactory' is null and therefore this DbProviderFactory can't be used. at SD.LLBLGen.Pro.ORMSupportClasses.DQEConfigurationBase.StoreDbProviderFactoryRegistration(Type factoryType, String invariantName) at SD.LLBLGen.Pro.DQE.SqlServer.SQLServerDQEConfiguration.AddDbProviderFactory(Type factoryType, String invariantName) at Program.<>c.<<Main>$>b__0_1(SQLServerDQEConfiguration c) in C:\_Projects\...\Program.cs:line 29 at SD.LLBLGen.Pro.ORMSupportClasses.RuntimeConfiguration.ConfigureDQE[SQLServerDQEConfiguration](Action`1 configureFunc) at Program.<Main>$(String[] args) in C:\_Projects\...\Program.cs:line 28 --- End of inner exception stack trace ---

When I debug and navigate to the SqlClientFactory-class, I end up in C:\Users\myuser.nuget\packages\system.data.sqlclient\4.8.3\ref\netcoreapp2.1\System.Data.SqlClient.dll which looks like a rather empty implementation of this factory.

Trying to register the factory with

DbProviderFactories.RegisterFactory("System.Data.SqlClient", SqlClientFactory.Instance);

causes an ArgumentNullException:* Value cannot be null. (Parameter 'factory')*

But I don't know how to solve the issue in my project with the info from the thread I mentioned. The problem is possibly not LLBLGen related, but as I find almost no info when I Google (or Bing) the issue, I hope clever LLBLGen-users can help me out here.

Does anyone know how to fix this SqlClientFactory being null?

Kind regards,

Peter

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 02-May-2022 09:24:02   

The static constructor of the factory has to run to set the instance, perhaps the webassembly/blazor nature of the app prohibits that? If you're planning on running the generated code + runtime on webassembly in a browser it might not work regardless btw.

I assume you've tried Microsoft.Data.SqlClient too? (which has a different namespace so you have to change the using statements).

In any case, the main issue is that the static constructor of the SqlClientFactory fails to create an instance or doesn't run. Why that happens is beyond me as you can see in the linked thread.

Frans Bouma | Lead developer LLBLGen Pro
PeterVD
User
Posts: 15
Joined: 16-Jan-2007
# Posted on: 02-May-2022 13:36:15   

Hi Frans,

Yes, I tried the Microsoft.Data.SqlClient too, with exactly the same result, unfortunately.

Ok, I'll have an extra look at it, and look if I can find a way to force the static constructor to run. Otherwise it's back to Entity Framework. :-/

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 02-May-2022 16:12:26   

PeterVD wrote:

Hi Frans, Yes, I tried the Microsoft.Data.SqlClient too, with exactly the same result, unfortunately. Ok, I'll have an extra look at it, and look if I can find a way to force the static constructor to run. Otherwise it's back to Entity Framework. :-/

EF has the same problem tho.

It might be possible to enforce the factory be created, by explicitly obtaining it in code first (so not just typeof).

Frans Bouma | Lead developer LLBLGen Pro