Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrating DbConnectionFactory from .net to.net core

Tags:

c#

.net

.net-core

I'm trying to migrate a small .net framework project to .net core one. But I've spent a couple of hours searching how to adopt the code below to .net core 2.0. But I was unlucky, because it seems that System.Data.Common functionality was cut for some reason. So, I've 2 problems here:

  1. How to get provider name from connection string? Do we have ConnectionStringSettings for .net core 2.0
  2. How can we get a factory base only on provider name string? Any other thought how to perform loosely coupled db connection factory

    public class ConnectionFactory
    {
        protected readonly DbProviderFactory Factory;
    
        protected readonly string ConnectionString;
    
        public ConnectionFactory(string connectionStringName)
        {
            var settings = ConfigurationManager.ConnectionStrings[connectionStringName];
            this.Factory = DbProviderFactories.GetFactory(settings.ProviderName);
            this.ConnectionString = settings.ConnectionString;
        }
    
        public DbConnection Connection
        {
            get
            {
                var con = Factory.CreateConnection();
                con.ConnectionString = this.ConnectionString;
                return con;
            }
        }
    }
    
like image 513
managerger Avatar asked Oct 24 '25 04:10

managerger


1 Answers

DbProviderFactories will be available in .net core 2.1 (2Q 2018).

like image 90
managerger Avatar answered Oct 25 '25 16:10

managerger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!