带有企业库6的网络核心1(dnx 4.5.1)-设置连接字符串 [英] net core 1 (dnx 4.5.1) with enterpriselibrary 6 - setting up the connection string

查看:117
本文介绍了带有企业库6的网络核心1(dnx 4.5.1)-设置连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在运行具有网络核心1(dnx 4.5.1)的企业库数据访问块时遇到很大的问题

i ve big problems running enterprise library data access block with net core 1 (dnx 4.5.1)

如何设置entlib的默认连接字符串

How can i setup the default connection string for entlib

我的appsettings.json "ConnectionString":服务器=本地主机\ sqlexpress;初始目录=盲目;用户ID =盲目;密码=盲目"

my appsettings.json "ConnectionString": "Server=localhost\sqlexpress;Initial Catalog=blind;User Id=blind;Password=blind"

这是我的问题(无默认连接字符串) 数据库db = DatabaseFactory.CreateDatabase();

Here is my problem (no default connectionstring) Database db = DatabaseFactory.CreateDatabase();

如何将appsettings ConnectionString传递给entlib数据库工厂

how can i pass the appsettings ConnectionString to the entlib databasefactory

任何帮助将不胜感激

推荐答案

我知道这是一个老问题,但是我有一个类似的设置(但是使用.NET Core 2.0),花了我一段时间才弄清楚如何设置默认数据库连接,而无需使用web.config对其进行管理.

I know it's an old question, but I have a similar setup (but using .NET Core 2.0) and it took me awhile to figure out how to set the default database connection without using the web.config to manage it.

我所做的是在appsettings.json中包含默认数据库和所有连接字符串,然后在启动类中,将appsettings.json读入一个对象,该对象定义为存储默认数据库名称和连接字符串并使用DatabaseFactory.SetDatabase配置默认的+命名数据库.

What I did was include the default database and all of the connection strings in the appsettings.json and then in my Startup class I read the appsettings.json into an object that I defined to store the default db name and the connection strings and configure the default + named database using DatabaseFactory.SetDatabase.

DatabaseFactory.SetDatabases()定义

public class DataConfiguration
{
    public string DefaultDatabase { get; set; }
    public List<ConnectionStringSettings> ConnectionStrings { get; set; }
}



public class Startup
{
    public Startup(IConfiguration configuration)
    {
        //Get the Database Connections from appsettings.json
        DataConfig = configuration.Get<DataConfiguration>(); 

        var defaultDb = DataConfig.ConnectionStrings?.Find(c => c.Name == DataConfig.DefaultDatabase);
        DatabaseFactory.SetDatabases(() => new SqlDatabase(defaultDb.ConnectionString), GetDatabase);

        Configuration = configuration;
    }

    public Database GetDatabase(string name)
    {
        var dbInfo = DataConfig.ConnectionStrings.Find(c => c.Name == name);

        if (dbInfo.ProviderName == "System.Data.SqlClient")
        {
            return new SqlDatabase(dbInfo.ConnectionString);
        }

        return new MySqlDatabase(dbInfo.ConnectionString);
    }

}

这篇关于带有企业库6的网络核心1(dnx 4.5.1)-设置连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆