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.