Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Entity Framework and connection strings

I have just started playing with the 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 on web.config with the DB details when you add a ADO entity data model. The problem is that I use ASP.NET user/privileges schema which uses "ApplicationServices" connection string which exists by default. All of those tables as well as my application tables exists on the same DB. I want the DB details(host/user/pass) to come from a single connection string entry. Or even if there are multiple connection string entries is it possible to get the DB details alone from a single source.

I come from the PHP world and couldn't figure out how to get it done from the other similar threads that I had bumped into.

Thanks

like image 590
Prasanth Avatar asked Aug 14 '26 00:08

Prasanth


2 Answers

Have you tried just renaming "testEntities" to "ApplicationServices"? So you'll end up with 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'll obviously need to tweak the EF settings to update the connection string name as well.

By the way: Entity Framework - redundant connection string

like image 96
Kon Avatar answered Aug 17 '26 03:08

Kon


Use simple approach. Define only ApplicationServices in the configuration and build EF connection string in the application. ObjectContext offers constructors with EntityConnection or a connection string directly. Pass ApplicationService's connection string to the provider connection string part in EF connection string.

EF offers defining connection string in configuration part but in 90% situations the only part which will change is provider connection string. Information about csdl, ssdl and msl files will usually not change. The only reason to change those resources can be supporting multiple database providers and in such case provider connection string part will change as well. In my opinion many applications can have those resource paths hardcoded directly in related context.

like image 38
Ladislav Mrnka Avatar answered Aug 17 '26 03:08

Ladislav Mrnka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!