实体框架 - 冗余连接字符串 [英] Entity Framework - redundant connection string

查看:192
本文介绍了实体框架 - 冗余连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用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

解决方案

/ div>


  1. 从普通连接字符串手动创建DbConnection

  2. 手动创建
  3. 创建 EntityConnection 使用此ctor

  4. 将实体连接传递给ObjectContext构造函数。


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=&quot;Data Source=localhost;Initial Catalog=VNK;User ID=user;Password=pass;MultipleActiveResultSets=True&quot;" 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

解决方案

  1. Manualy create DbConnection from ordinary connection string
  2. Manualy create MetadataWorkspace object.
  3. Create EntityConnection using this ctor.
  4. Pass entity connection to ObjectContext constructor.

这篇关于实体框架 - 冗余连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆