开发者

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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜