ASP.NET Entity Framework and Connection Strings

I just started playing with ASP.NET MVC.

<add name="ApplicationServices" connectionString="Data Source=localhost;Port=3306;Database=test;User id=root;Password=admin;" providerName="MySql.Data.MySqlClient" /> <add name="testEntities" connectionString="metadata=res://*/Models.testDB.csdl|res://*/Models.testDB.ssdl|res://*/Models.testDB.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=localhost;port=3306;database=test;User Id=root;password=admin&quot;" providerName="System.Data.EntityClient" /> 

VS automatically creates connection strings in the web.config file with database data when adding an ADO entity data model. The problem is that I am using the ASP.NET user / privilege scheme, which uses the "ApplicationServices" connection string, which exists by default. All these tables, as well as my application tables, exist in the same database. I want the database data (host / user / pass) to come from one line of the connection string. Or even if there are several lines of connection string, you can get database data separately from one source.

I came from the world of PHP and could not figure out how to do this from other similar threads that I came across.

thanks

+6
source share
2 answers

Have you tried simply renaming "testEntities" to "ApplicationServices"? This way you get one connection string:

 <add name="ApplicationServices" connectionString="metadata=res://*/Models.testDB.csdl|res://*/Models.testDB.ssdl|res://*/Models.testDB.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=localhost;port=3306;database=test;User Id=root;password=admin&quot;" providerName="System.Data.EntityClient" /> 

You will probably need to change the EF settings to update the connection string name.

By the way: Entity Framework - redundant connection string

+1
source

Use a simple approach. Define only ApplicationServices in the configuration and create an EF connection string in the application. ObjectContext offers constructors with EntityConnection or a connection string directly. Pass the ApplicationService connection string to the provider connection string line in the EF connection string.

EF offers a definition of the connection string in the configuration part, but in 90% of situations, the only part that will change is the provider connection string. Information about the csdl, ssdl and msl files is usually not changed. The only reason to change these resources is to support multiple database providers, in which case the provider connection string string will also change. In my opinion, many applications can hardcode these resource paths in the appropriate context.

+1
source

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


All Articles