简体   繁体   English

减少Entity Framework库对app.config的依赖

[英]Reducing Entity Framework library's dependency on app.config

I am looking for some advice on how to reduce my Entity Framework library's dependency on app or web.config files. 我正在寻找一些关于如何减少我的Entity Framework库对app或web.config文件的依赖性的建议。 My app containins an ADO.NET Entity Data Model. 我的应用程序包含一个ADO.NET实体数据模型。

I currently reference this library in a web project, but in order to do so, I must add the connection string to the web app's web.config before deploying. 我目前在Web项目中引用了这个库,但为了做到这一点,我必须在部署之前将连接字符串添加到Web应用程序的web.config中。

I now have a requirement to use the standalone DLL and am unable to alter the parent project's configuration files to include that of my EF library. 我现在需要使用独立DLL,并且无法更改父项目的配置文件以包含我的EF库的配置文件。

What would be the best way to store my connection string, yet maintain the integrity of my edmx and where should I reference it? 存储我的连接字符串的最佳方法是什么,同时保持我的edmx的完整性以及我应该在哪里引用它?

EntityConnectionStringBuilder builder = New EntityConnectionStringBuilder();
builder.Metadata =
    "res://*/Entities.VDMFinance.csdl|res://*/Entities.YourEntities.ssdl|res://*/Entities.YourEntities.msl";
builder.Provider = "System.Data.SqlClient";
builder.ProviderConnectionString = yourConnectionString;
Dim dc As New YourEntities(builder.ConnectionString)
dc.CommandTimeout = 0;

You can initialize all your data contexts from static factory that does this all the time. 您可以从静态工厂初始化所有数据上下文,并始终执行此操作。 You can get the metadata and provider from the initial connection string that gets generated when you create the entities. 您可以从创建实体时生成的初始连接字符串中获取元数据和提供程序。

Specifying configuration in the main web.config / app.config is common for all .NET applications. 在主web.config / app.config中指定配置对于所有.NET应用程序都是通用的。 Even large products use it. 即使是大型产品也使用它。 Trying to avoid it doesn't have any benefit. 试图避免它没有任何好处。

If you want your .dll to manage connection string it must read it from somewhere - probably from custom configuration file with file name hardcoded in your library. 如果您希望.dll管理连接字符串,则必须从某处读取它 - 可能来自库中硬编码文件名的自定义配置文件。 Once you have connection string loaded you can pass it to constructor of your ObjecContext as @linkerro showed. 加载连接字符串后,您可以将其传递给ObjecContext构造函数,如@linker所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM