ASP.NET 5 / MVC 6 Database First - a connection string named MyEntity was not found in the application configuration file

Our ASP.NET 5 / MVC 6 project accesses our database through a business-level build, and then into a DAL (Database First) build. MVC 6 code explodes when trying to execute business-level code that uses DbContext to access the database with an error: a line with the name "MyEntity" cannot be found in the application configuration file. I tried to define the MyEntity connection string in the MVC project config.json, appsettings.json in different ways, no luck.

If you use business-level methods from the tester project, where I have the expected app.config file with the connection string syntax as shown below, this is not a problem.

Note This question could also be reformulated as porting an MVC 5 application to MVC 6, where access to a weak database connection with MVC 5 to the DAL layer and the MVC code did not know about EF and ONLY supplied the web.config connection string needed by the DbContext object in the DAL?

Any help would be appreciated, let me know if you need more information.

The syntax of the configuration file in the Tester project is:

<add name="MyEntity" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=MyServer;initial catalog=MyDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> 

Probable incorrect syntax in config.json file.

 { "Data": { "defaultConnection": { "connectionString": "metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=MyServer;initial catalog=MyDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" }, "entityFramework": { "MyEntity": { "ConnectionString": "name=data:defaultConnection:connectionString" } } } } 
+5
source share
5 answers

How is the database context created?

Transferring configuration settings from web.config

The connection string can be in appsettings.json

 { "Data": { "DefaultConnection": { "ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;" } } } 

Edit You can get the connection string as follows:

 string connection = Configuration.Get<string>("Data:DefaultConnection:ConnectionString"); 
+1
source

Add the class to the data layer as

 public class ConfigHelper { public static string ConnectionString { get; set; } public ConfigHelper(string conn ) { ConnectionString = conn; } } 

Then in startup.cs

 var connectionString = Configuration["Data:"Connectionstring name":ConnectionString"]; ConfigHelper.ConnectionString = connectionString; 

now in

 public partial class Context : DbContext { public Context() : base(ConfigHelper.ConnectionString) { } } 

another approach would be to use dependency injection.

+1
source

Many thanks to Rami for the helpful hints, I really relied on EF to automatically pick up the connection string. This worked fine for MVC 5 (and previous versions) working with the First First database, where we could specify the expected name of the DB connection, but with MVC 6 we need to explicitly pass the connection string.

I added an additional ctor that takes the connection string into my own partial class file, since we are doing Database First, and the context file is overwritten with subsequent model updates from the database. I. Having in a separate partial class / file, a new ctor is saved so as not to be overwritten.

 public partial class MyDbContext : DbContext { public MyDbContext(string connectionString) : base(connectionString) { } } 
0
source

Setting the connection in config.json does not work for me. We need to move the connection string from the app.config file to the code. This can be done in the context.cs file. In the constructor you need to specify a connection string

 public partial class ProducerCentralEntities : DbContext { public ProducerCentralEntities() : base(@"metadata = res://*/TestProj.csdl|res://*/TestProj.ssdl|res://*/TestProj.msl;provider=System.Data.SqlClient;provider connection string=';data source=serverName;initial catalog=databaseName;integrated security=True;MultipleActiveResultSets=True;application name=EntityFramework';") { } } 

Note: 1) You must have a full connection string 2) Replace the quote with 'c'

0
source

Place the connection string in the app.config file.

0
source

Source: https://habr.com/ru/post/1242826/


All Articles