企业库数据库从web.config连接到Asp.net 5.0 Core vnext MVC6

时间:2016-05-02 12:29:10

标签: c# asp.net-core asp.net-core-mvc enterprise-library

如果使用Microsoft.Practices.EnterpriseLibrary,则使用web.config文件设置数据库配置和连接。如何使用MVC6 .Net Core / Vnext?为了让这个工作?是否需要在启动文件上设置某些内容??

的Web.Config

<configSections>
<section name="dataConfiguration"    type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings,   Microsoft.Practices.EnterpriseLibrary.Data, Version=6.0.0.0, Culture=neutral,   PublicKeyToken=31bf3856ad364e35" requirePermission="false" />
  </configSections>
<dataConfiguration defaultDatabase="MyDBConnectionString" />
<connectionStrings>
<add name="MyDBConnectionString" connectionString="Data Source=MyDatabaseServer;Database=MyDatabase; Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
</connectionStrings>

appsettings.json

{
 "Data": {
   "dataConfiguration": {
     "type":"Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35",
       "requirePermission": "false"
},
"DefaultConnection": {
  "ConnectionString": "Data Source=MyDatabaseServer;Database=MyDatabase; Integrated Security=SSPI;",
  "providerName": "System.Data.SqlClient"
 }
},

数据访问

    public static Model.Record GetRecordByID(Int32 ID)
    {

        //Declare the variables
        Amendment obj = new Record();
        object xml = null;
        DbCommand dbCmd = null;
        Database db = null; 
        try
        {

            // Create the database object, using the default database service. The
            // default database service is determined through configuration.


            DatabaseProviderFactory factory = new DatabaseProviderFactory(new SystemConfigurationSource());
            DatabaseFactory.SetDatabaseProviderFactory(factory, false);


           db = DatabaseFactory.CreateDatabase();


             dbCmd = db.GetStoredProcCommand("spXMLGetRecordByID");

            db.AddInParameter(dbCmd, "RecordID", DbType.Int32, ID);
            db.AddInParameter(dbCmd, "@xml", DbType.Xml, 1);



            if (!xml.Equals(DBNull.Value))
            {


                obj = (Amendment)Deserialize(xml.ToString(), typeof(Record));
            }


        }
        catch (SqlException sqlEx)
        {
            // ExceptionUtility.SendError(sqlEx, "DAL", "GetRecordByID");
        }
        catch (Exception ex)
        {
            //ExceptionUtility.SendError(ex, "DAL", "GetRecordByID");

        }
        finally
        {
            // DALCleanUp(db, dbCmd);

        }

        return obj;

    }

2 个答案:

答案 0 :(得分:0)

对于所有试图在ASP.NET Core项目中使用EnterpriseLibrary.NetCore的人:

仅当将配置存储在app.config文件中时,才会读取该配置。

如果要将项目从ASP.NET迁移到ASP.NET Core,则必须将web.config重命名为app.config。

另请参阅: https://stackoverflow.com/a/50572248/7500260

答案 1 :(得分:-1)

要使用appsettings.json(或任何其他json文件)中的设置,第一步是将文件添加到Startup类中的IConfigurationRoot成员变量中。使用ASP.NET 5模板创建项目时,将为您生成此代码。

public IConfigurationRoot Configuration { get; set; }

public class Startup
{
    var builder = new ConfigurationBuilder()
                    .AddJsonFile("appsettings.json")
                    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

    builder.AddEnvironmentVariables();
    Configuration = builder.Build();
}

之后,您可以使用IConfigurationRoot成员变量从json文件中访问各个值。

this.Configuration["Data:DefaultConnection:ConnectionString"]

如果您的类与json匹配,您也可以获取配置文件的各个部分。

Public class DataConfiguration
{
    public string type { get; set; }
    public bool RequirePermission { get; set; }
}

在Startup类的ConfigureServices()方法中,将该类与json文件中的值一起添加到IoC容器中。

services.Configure<DataConfiguration>(Configuration.GetSection("Data:dataConfiguration"));

这是在Startup

类的Configure()方法中访问这些设置的方法
DataConfiguration dataConfig;
bool requirePermission;

dataConfig= app.ApplicationServices.GetService<DataConfiguration>();
requirePermission = dataConfg.RequirePermission ;

您还可以从框架调用的任何类访问设置(因为您的实例化类位于IoC容器中)。

public class MyController : Controller
{
    private IOptions<DataConfiguration> dataConfig;

    public MyController(IOptions<DataConfiguration> dataConfig)
    {
        this.dataConfig= dataConfig;
    } 

    ...
}