3
votes

We have an existing .NET Framework library with Entity Framework 6 and static methods like this:

public class OrderManager
{
    public static OrderDTO GetOrderByOrderId(int oid)
    {
        var entities = new MyEntities();
        ....
    }
}

where MyEntities have a hardcoded connectionstring name

internal partial class MyEntities : DbContext
{
    public SSE3Entities() : base("name=MyEntities")   {}
}

When used in a ASP.NET application, the web.config have a connectionstrings defined like this:

<connectionStrings>
   <add name="MyEntities" connectionString="metadata=res://*/M..." providerName="System.Data.EntityClient" />
</connectionStrings>

But how can I reuse this library in a aspnet core application. Tried:

{
  "ConnectionStrings": {
    "MyEnties": "metadata=res://*/M...",
  }, 
  "Logging": {
     ...
  }
}

I know passing the connectionstring into the OrderManager constructor is what we should have done years ago, but changing it now requiers a lot of work.

Are there any ways we can make EF read the new configurations system?

Or could we maybe write some settings to the old ConfigurationManager?

I tried dropping in a web.config without any luck

2
Is it really that much work to convert your app to use the new DI system? - DavidG
@DavidG This library is used by our main app, and it contains about 50+ "OrderManagers" - which in turn are referenced by each other, so yes, rewriting that would atleast generate a lot of changes. I was hoping to reuse the library without any changes in a new dotnet core webapi, and then, part by part migrate features and finally remove the old library - Larsi
I would still recommend spending the time to remove that old technical debt. You'll have to do it eventually, so best get it out of the way now before building additional code on top of it. - DavidG
@DavidG Thanks, I'll follow that advice - Larsi
@ArunPratap don't use inline code to highlight random terms. - CodeCaster

2 Answers

0
votes

add System.Configuration.ConfigurationManager from nuget

add a file app.config

.net core applictaion is a consle app, so it find app.config first

app.config:

<appSettings>
    <add key="webpages:Version" value="2.0.0.0"/>
</appSettings>
0
votes

This solution will work for ASPNET Core MVC 2.0, 2.2 and 3.0 In your app root go find the appsetting.json file and modify the connection string.

 {
  "ConnectionStrings": {
    "DefaultConnection": "Data Source=DESKTOP-3R4MR9H\\SQLSERVER3R4MR9;Initial 
     Catalog=ASPNetCoreMVC;Persist Security Info=True;User 
     ID=sa;Password=********;"
  },
  "Logging": {
    "LogLevel": {
       "Default": "Warning"
     }
  },
  "AllowedHosts": "*"
}

Work Around[1]