EF Core2.2: Scaffold-DbContext not working with named connection string in WPF project

c# entity-framework entity-framework-core

Question

I want to work with EF Core 2.2 and Database first in my .net Framework 4.7 project. it works fine running this command:

Scaffold-DbContext "data source=dbServer;initial catalog=myData;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Database

but then MS puts down this warning:

warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.

So i thought I do as told and updated the script as follows:

Scaffold-DbContext "name=MyConnection" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Database

which results in this error:

A named connection string was used, but the name 'MyConnection' was not found in the application's configuration. Note that named connection strings are only supported when using 'IConfiguration' and a service provider, such as in a typical ASP.NET Core application. See https://go.microsoft.com/fwlink/?linkid=850912 for more information.

The named connection itself works fine, I have tested that by commennting out the first line that was generated by EF core and making use of the second line:

//optionsBuilder.UseSqlServer("data source=dbServer;initial catalog=myData;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework");
            optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString);

starting the program it can connect and display the data as expected. But I would prefer to have that setting alrady in Scaffold-DbContext. Any ideas?

For completeness sake here's the app.config:

<?xml version="1.0" encoding="utf-8"?>
  <configuration>
    <connectionStrings>
       <add name="MyConnection" connectionString="data source=dbServer;initial catalog=myData;integrated security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
    </connectionStrings>
 </configuration>
...
1
4
8/29/2019 12:48:57 PM

Accepted Answer

EF Core2.2: Scaffold-DbContext not working with named connection string in WPF project

Fact. Both error message

Note that named connection strings are only supported when using IConfiguration and a service provider, such as in a typical ASP.NET Core application.

and Scaffold-DbContext command documentation for -Connection parameter

...For ASP.NET Core 2.x projects, the value can be name=<name of connection string>. In that case the name comes from the configuration sources that are set up for the project...

indicate that named connection strings cannot be used with Scaffold-DbContext command in "legacy" app.config (NET Framework) based projects.

So for Scaffold-DbContext you have to use the actual connection string. And named connection string here (where I guess was the warning):

optionsBuilder.UseSqlServer(
    ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString);
2
8/29/2019 1:19:38 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow