2

I've ported an app I wrote in .NET Core 2.1 to .NET 4.7.2.

Basically almost everything was OK, a couple things were broken (as expected) but there is one thing that doesn't seem to be agnostic across netcore and .net.

And the line is:

DbProviderFactories.RegisterFactory("test", SqlClientFactory.Instance);
var db = new Database("Server=TCPblablabla login", "test");

SqlSet <CharObj> products = db.From<CharObj>("[MyDb].[dbo].[Whatever]").Where("Name = {0}", myvar);

It works perfectly well on .netcore, however, it does not work on .Net 4.7.2. Because the DbProviderFactories.RegisterFactory does not exist in 4.7.2.

I've seem a lot of people complaining about it, but I could not yet find any override etc. Do you guys have ANY idea on how to solve this issue?

.Net 4.7.2 - Does not exist

.Netcore - Works like a charm!

PS: It was not supposed to happen, I opened an issue on GitHub and they fixed it a couple of weeks after and closed my ticket, it was even shown in their C# news conference (I don't remember which).

layer07
  • 53
  • 2
  • 8

2 Answers2

2

The way to switch providers in .Net Framework was with this line:

var dbProvider = DbProviderFactories.GetFactory("System.Data.MySql")

Here are a couple of good links that show how to set up the DbProviderFactory the old way:

https://blog.codeinside.eu/2016/12/31/dbproviderfactory-write-database-agnostic-adonet-code/

https://weblog.west-wind.com/posts/2017/nov/27/working-around-the-lack-of-dynamic-dbproviderfactory-loading-in-net-core

Hope this gets you on the right track!

ingep
  • 187
  • 1
  • 2
  • 10
2

The DbProviderFactories class was designed a long time ago and registering factories must be done via the app configuration file on .NET Framework. The RegisterFactory method was only added later on .NET Core and never backported to .NET Framework.

But there exists a way to add a DbProviderFactory programmatically by tricking the configuration system on .NET Framework, see Add a DbProviderFactory without an App.Config to learn how to do it.

0xced
  • 25,219
  • 10
  • 103
  • 255