具有企业库数据(EnterpriseLibrary.Data.NetCore)和JSON的dotNet核心(控制台应用程序) [英] dotNet core (console app) with Enterprise Library Data (EnterpriseLibrary.Data.NetCore) and JSON

查看:65
本文介绍了具有企业库数据(EnterpriseLibrary.Data.NetCore)和JSON的dotNet核心(控制台应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用:

EnterpriseLibrary.Data.NetCore

EnterpriseLibrary.Data.NetCore

.nuget \ packages \ enterpriselibrary.data.netcore \ 6.0.1313

.nuget\packages\enterpriselibrary.data.netcore\6.0.1313

https://www.nuget.org/packages/EnterpriseLibrary.Data.NetCore/

.NET Core 2.1

.NET Core 2.1

我有以下JSON

{
  "ConnectionStrings": {
    "MyDefaultConnectionName": "Server=.\\MyInstance;Database=MyDB;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}

并且此代码工作正常(在我的Program.cs dotnet控制台应用程序中)(简单地显示出正在拾取我的appsettings.json,并且看起来我的连接字符串json结构看起来正确.

And this code works fine (in my Program.cs dotnet console app) (simple showing my appsettings.json is being picked up, and it looks like my connection-string json structure looks right.

using Microsoft.Extensions.Configuration;

            IConfiguration config = new ConfigurationBuilder()
                    .SetBasePath(System.IO.Directory.GetCurrentDirectory())
                    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                    .Build();


            string conString = Microsoft
               .Extensions
               .Configuration
               .ConfigurationExtensions
               .GetConnectionString(config, "MyDefaultConnectionName");

            Console.WriteLine("MyDefaultConnectionName.conString='{0}'", conString);

我看到:

MyDefaultConnectionName.conString ='Server =.\ MyInstance; Database = MyDB; Trusted_Connection = True; MultipleActiveResultSets = true'

MyDefaultConnectionName.conString='Server=.\MyInstance;Database=MyDB;Trusted_Connection=True;MultipleActiveResultSets=true'

但是,我使用该代码的尝试失败了:

However, my attempt to use the code is failing:

using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Data.Common;

    public DataSet GetADataSet()
    {
        DataSet returnDs = null;
        string sql = "Select * from dbo.MyTable";

        try
        {

            DatabaseProviderFactory factory = new DatabaseProviderFactory();
            Database db = DatabaseFactory.CreateDatabase("MyDefaultConnectionName");
            DbCommand dbc = db.GetSqlStringCommand(sql);
            returnDs = db.ExecuteDataSet(dbc);
        }
        catch (Exception ex)
        {
            string temp = ex.Message;
            throw ex;
        }

        return returnDs;
    }

例外是:

运行从dbo.MyTable中选择*"时发生异常(MyDefaultConnectionName)(未为静态DatabaseFactory设置数据库提供程序工厂.设置提供程序工厂以调用DatabaseFactory.SetProviderFactory方法或通过调用DatabaseFactory指定自定义映射.SetDatabases方法.')

Exception while running 'Select * from dbo.MyTable' ('MyDefaultConnectionName') ('Database provider factory not set for the static DatabaseFactory. Set a provider factory invoking the DatabaseFactory.SetProviderFactory method or by specifying custom mappings by calling the DatabaseFactory.SetDatabases method.')

我找到了此链接....

I found this link....

Microsoft.Practices.EnterpriseLibrary.Data.DLL,但未在用户代码中处理

基于"较旧的dotnet核心xml前版本.

to "older" pre dotnet core xml based.

我也去过项目网站

https://github.com/Chavoshi/EnterpriseLibrary.NetCore

但找不到有效的示例

...关于我上面的代码行:

..... in regards to my line of code above:

DatabaseProviderFactory myFactory =新的DatabaseProviderFactory();

DatabaseProviderFactory myFactory= new DatabaseProviderFactory();

和错误消息:

DatabaseFactory.SetProviderFactory

DatabaseFactory.SetProviderFactory

myFactory没有方法SetProviderFactory

myFactory does not have a method SetProviderFactory

........

我尝试过...(但是我知道它确实与xml属性不同(以后的读者,请不要在下面的json上浪费时间)

I tried this...(but I know it really isn't the same as the xml attribute (future readers, don't waste your time with the below json)

{
  "ConnectionStrings": {
    "MyDefaultConnectionName": "Server=.\\MyInstance;Database=MyDB;Trusted_Connection=True;MultipleActiveResultSets=true",
    "providerName": "System.Data.SqlClient"
  }
}

使用什么神奇的语法糖

https://github.com/Chavoshi/EnterpriseLibrary.NetCore

使用dotnet核心控制台应用程序?使用JSON

with a dotnet core console app? WITH JSON

APPEND:

根据评论,我也跟踪了这个示例:

Based on the comments, I also have chased this example:

https://github.com/Chavoshi/EnterpriseLibrary.NetCore/tree/master/Examples

一个例子是基于xml的,而不是基于json的.

The one example there is xml based, not json based.

推荐答案

感谢您的解释.正如@ panagiotis-kanavos在评论中还提到的那样,向库中添加JSON支持是一项重大更改,需要付出很多努力,而且我肯定不能单独完成.

Thanks all for your explanations. As @panagiotis-kanavos also mentioned in the comments, adding JSON support to the library is a major change and needs a lot of efforts and for sure I cannot do it individually.

顺便说一句,我将以某种方式启动企业图书馆社区,并使它再次恢复生命,就像我以前那样(并且一直是它的粉丝,因为我个人认为它非常方便,尤其是在 Exception Handling Logging 块中-因此绝对添加JSON支持将是其中的一个新的主要版本中的第一个更改.

BTW I am going to somehow start the Enterprise Library Community and make it alive again -as I was (and am) a fan of it always, because I personally think it's really handy, specially in Exception Handling and Logging blocks- so absolutely adding JSON support will be one of the very first changes in the new major versions.

但是目前,该项目只是在帮助那些希望将旧项目转换为.Net Core的人,而他们所面临的挑战却更少,而无需更改基础架构.

But for now this project is just to help the ones who want to convert their old projects to .Net Core with less challenges, without changing the infrastructure.

最后,我很高兴有人在社区中为我提供帮助,因为我已开始与Microsoft讨论此主题,并等待他们的决定让我用新版本更新官方软件包,以防万一继续更新库.

At the end, I appreciate if someone will help me on the community as I have started to discuss with Microsoft on this subject and waiting for their decision to let me update the official packages with the new versions in case if we are going to continue on updating the library.

这篇关于具有企业库数据(EnterpriseLibrary.Data.NetCore)和JSON的dotNet核心(控制台应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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