Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
...
like image 699
Jan Avatar asked Aug 29 '19 12:08

Jan


1 Answers

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);
like image 198
Ivan Stoev Avatar answered Sep 18 '22 14:09

Ivan Stoev