我在我的项目中使用实体框架 4。该框架创建了自己的连接字符串,所以我的web.config
connectionStrings 部分文件如下所示:
<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>
第一个连接字符串称为ApplicationServices
是我原来的。第二个,叫做VNKEntities
已在生成模型时创建。
当我检查生成的*.edmx文件中,我发现该模型正在引用其连接字符串,如下所示:
/// <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();
}
我的问题是我怎样才能摆脱VNKEntities
连接字符串,只保留ApplicationServices
,我将从我的模型中引用它?我只想有一个到数据库的连接字符串,因为我只使用一个数据库(将构造函数参数替换为name=VNKEntities
to name=ApplicationServices
不管用)。
Regards
尽管您可以在代码中创建连接,正如 @gandjustas 指出的(+1),但您无法摆脱连接字符串或EntityConnection
.
这是因为它实际上并不多余。是的,数据库连接部分是多余的,@gandjustas 向您展示了如何删除该冗余。但是,实体框架连接字符串还包含有关您的模型的信息,这些信息在您希望保留的连接字符串中的任何位置都找不到。该模型信息必须来自某个地方。您是否消除了实体框架的连接字符串并使用参数列表构造函数ObjectContext
,您将消除对该模型的所有引用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)