dotNet core (console app) with Enterprise Library Data (EnterpriseLibrary.Data.NetCore) and JSON

喜夏-厌秋 提交于 2021-02-10 06:27:06

问题


I am trying to use:

EnterpriseLibrary.Data.NetCore

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

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

.NET Core 2.1

I have the following JSON

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

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);

and I see:

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;
    }

The exception is:

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 but was not handled in user code

to "older" pre dotnet core xml based.

I've been too the project website

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

but cannot find a working example

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

DatabaseProviderFactory myFactory= new DatabaseProviderFactory();

and the error message:

DatabaseFactory.SetProviderFactory

myFactory does not have a method SetProviderFactory

........

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"
  }
}

What is the magic syntax sugar for using

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

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

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


回答1:


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.

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.

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.

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.



来源:https://stackoverflow.com/questions/53263204/dotnet-core-console-app-with-enterprise-library-data-enterpriselibrary-data-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!