Using EF (not EF Core) with .NET 6 or above

51 Views Asked by At

I am creating an ASP.NET Core 6 Web API, but I have some legacy libraries I have to use that are dependent on EF 6 (not EF Core).

I see in those libraries dbcontext is getting initialised with parameterless constructors i.e. not passing connection string, dbOptions, etc.

I see it defaulting to localdb for dbserver.

 using (var context = new AppSettingDb())
 {
     var item = context.AppSettings.FirstOrDefault(x => x.SettingName == settingName);
     if (item == null) throw new AppSettingException() { AppName = _appName, SettingName = settingName };
     return item.Value;
 }

Is there anyway of changing the default connection string to be used when context is created? I tried to add a web.config to my .NET 6 project, it did not work.

1

There are 1 best solutions below

1
Kęstutis Ramulionis On
public partial class AppSettingDb : DbContext
{
    public AppSettingDb(String? connectionString = null)
        : base(connectionString ?? "server=localdb;database=db")
    {
    }
}

Not quite sure, but is this what you are looking for?