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

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

问题描述

我使用实体框架4在我的项目。该框架已经建立了自己的连接字符串,所以我的的web.config connectionStrings节文件如下所示:

 <的ConnectionStrings>
    <添加名称=ApplicationServices的connectionString =数据源=本地主机;用户ID =用户; PWD =通;初始目录= VNK的providerName =System.Data.SqlClient的/>
    <添加名称=VNKEntities connectionString="metadata=res://*/VNKModel.csdl|res://*/VNKModel.ssdl|res://*/VNKModel.msl;provider=System.Data.SqlClient;provider连接字符串=安培; QUOT;数据源=本地主机;初始目录= VNK;用户ID =用户;密码=通; MultipleActiveResultSets =真放; QUOT;的providerName =System.Data.EntityClient/>
  < /的ConnectionStrings>
 

称为第一连接字符串 ApplicationServices 是我原来的。第二,所谓的 VNKEntities 而生成的模型已经建立。

当我检查了产生的 * EDMX 文件中,我发现,这种模式是参考它的连接字符串,如下所示:

  ///<总结>
    ///初始化使用中的应用程序配置文件中的VNKEntities一节中的连接字符串的新VNKEntities对象。
    ///< /总结>
    公共VNKEntities():基地(NAME = VNKEntities,VNKEntities)
    {
        this.ContextOptions.LazyLoadingEnabled = TRUE;
        OnContextCreated();
    }
 

我的问题是我怎么能摆脱 VNKEntities 连接字符串,只留下 ApplicationServices ,向其中我会从我的模型参考?我想只有一个连接字符串到数据库,因为我只使用一个数据库(从名称= VNKEntities 替换构造函数的参数,以 NAME = ApplicationServices 不工作)。

问候

解决方案
  1. Manualy从普通的连接字符串创建的DbConnection
  2. Manualy创建 MetadataWorkspace 对象
  3. 在使用创建 EntityConnection 这个构造函数
  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天全站免登陆