实体框架 - 冗余连接字符串 [英] Entity Framework - redundant connection string
问题描述
我在我的项目中使用Entity Framework 4。框架已经创建了自己的连接字符串,所以我的 web.config
connectionStrings节文件看起来如下:
< connectionStrings>
< add name =ApplicationServicesconnectionString =data source = localhost; user id = user; pwd = pass; initial catalog = VNKproviderName =System.Data.SqlClient/>
< add name =VNKEntitiesconnectionString =metadata = res://*/VNKModel.csdl | res://*/VNKModel.ssdl | res://*/VNKModel.msl; provider = System .Data.SqlClient; provider connection string =& quot; Data Source = localhost; Initial Catalog = VNK; User ID = user; Password = pass; MultipleActiveResultSets = True& providerName =System.Data.EntityClient/>
< / connectionStrings>
第一个连接字符串 ApplicationServices
原来一个。
当我检查生成的 *
时, .edmx 文件,我发现这个模型引用了它的连接字符串,如下所示: /< summary>
///使用应用程序配置文件的VNKEntities部分中的连接字符串初始化新的VNKntities对象。
///< / summary>
public VNKEntities():base(name = VNKEntities,VNKEntities)
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
我的问题是如何摆脱 VN_Fntities
连接字符串,只保留 ApplicationServices
,我将从我的模型引用它?我想只有一个连接字符串到数据库,因为我只使用一个数据库(替换构造函数参数从 name = VNKEntities
到
I'm using Entity Framework 4 in my project. The Framework has created its own connection string, so my web.config
connectionStrings section file looks following:
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=localhost;user id=user;pwd=pass;initial catalog=VNK" providerName="System.Data.SqlClient" />
<add name="VNKEntities" connectionString="metadata=res://*/VNKModel.csdl|res://*/VNKModel.ssdl|res://*/VNKModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=localhost;Initial Catalog=VNK;User ID=user;Password=pass;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
The first connection string called ApplicationServices
is my original one. The second, called VNKEntities
has been created while generating the model.
When I checked the generated *.edmx file, I found that this model is referencing its connection string, which is shown below:
/// <summary>
/// Initializes a new VNKEntities object using the connection string found in the 'VNKEntities' section of the application configuration file.
/// </summary>
public VNKEntities() : base("name=VNKEntities", "VNKEntities")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
My question is how can I get rid of the VNKEntities
connection string, and leave only ApplicationServices
, to which I will be referencing from my model? I would like to have only one connection string to the database, because I'm using only one database (replacing the constructor parameter from name=VNKEntities
to name=ApplicationServices
is not working).
Regards
- Manualy create DbConnection from ordinary connection string
- Manualy create MetadataWorkspace object.
- Create EntityConnection using this ctor.
- Pass entity connection to ObjectContext constructor.
这篇关于实体框架 - 冗余连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!