获得从其他位置的实体框架连接字符串? [英] Get Entity Framework connection string from alternate location?

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

问题描述

我怎样才能检索自定义配置文件中的实体框架4连接字符串,而不是Web.config文件?

编辑:
它是合理的,删除默认的构造函数生成的code和重建它在部分班级使用在连接字符串拉?

我真的想避免更改与重载的方法,包括连接字符串EF上下文中的所有引用。

@BrokenGlass:这就是我们结束了:

 公共部分类STARSEntities
{
    私人常量字符串_connectionStringFormat = @\"metadata=res://*/STARS.EntityModel.STARSModel.csdl|res://*/STARS.EntityModel.STARSModel.ssdl|res://*/STARS.EntityModel.STARSModel.msl;provider=System.Data.SqlClient;provider连接字符串='数据源= {0}; MultipleActiveResultSets = TRUE';    ///<总结>
    ///初始化使用STARS.xml配置文件中找到的连接字符串的新对象STARSEntities。
    ///< /总结>
    ///<&言论GT;
    ///如果STARSEntities类是从数据库再生,默认的构造需要从所生成的文件中删除。
    ///< /言论>
    公共STARSEntities():基座(GetConnectionString(),STARSEntities)
    {
        this.ContextOptions.LazyLoadingEnabled = TRUE;
        OnContextCreated();
    }    私人静态字符串GetConnectionString()
    {
        返回的String.Format(_connectionStringFormat,ApplicationConfiguration.GetConnectionString(星星));
    }}


解决方案

有一个构造函数重载为的DataContext ,你可以通过一个连接字符串 - 在这种情况下,你可以采取设置从任何地方你喜欢的。

修改的基础上更新的问题:


  

我真的想避免更改
  与EF上下文中的所有引用
  一个重载的方法,包括
  连接字符串。


问题是由T4脚本创建的实体背景下产生用作连接字符串常量属性

 公共常量字符串的ConnectionString =NAME = FooEntities;    公共FooEntities()
        :基地(ConnectionString,则容器名称)
    {
        this.ContextOptions.LazyLoadingEnabled = TRUE;
    }

既然你不能覆盖部分类的默认构造函数,你唯一的选择是改变了T4脚本本身 - 你应该看到在你.TT脚本文件如下:

 公开<#= code.Escape(集装箱)#>()
        :基地(ConnectionString,则容器名称)
    {
<#
        WriteLazyLoadingEnabled(容器);
#>
    }

要强迫你的连接字符串中使用,你可以修改构造函数调用通过调用你在一个单独的文件中定义(一个静态方法,但对于相同的部分类 FooEntities

 公开<#= code.Escape(集装箱)#>()
        :基座(GetCustomConnectionString(),容器名称)
    {
<#
        WriteLazyLoadingEnabled(容器);
#>
    }

现在 GetCustomConnectionString()可分别定义

 公共部分类FooEntities:ObjectContext的
{
   公共静态字符串GetCustomConnectionString()
  {
     返回Foobar的;然而//你想在这里确定连接字符串
  }
}

您看到这个越来越复杂和脆弱的非常快,所以我不建议这样做 - 但你可以

How can I retrieve the Entity Framework 4 connection string from a custom config file, not web.config?

Edit: Is it reasonable to delete the default constructor generated code and recreate it in a partial class to use the pulled in connection string?

I would really like to avoid changing all references to the EF context with an overloaded method including the connection string.

@BrokenGlass: This is what we ended up with:

public partial class STARSEntities
{
    private const string _connectionStringFormat = @"metadata=res://*/STARS.EntityModel.STARSModel.csdl|res://*/STARS.EntityModel.STARSModel.ssdl|res://*/STARS.EntityModel.STARSModel.msl;provider=System.Data.SqlClient;provider connection string='Data Source={0};MultipleActiveResultSets=True'";

    /// <summary>
    /// Initializes a new STARSEntities object using the connection string found in the STARS.xml configuration file.
    /// </summary>
    /// <remarks>
    /// If the STARSEntities class is regenerated from the database, the default constructor needs to be removed from the generated file.
    /// </remarks>
    public STARSEntities() : base(GetConnectionString(), "STARSEntities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }

    private static string GetConnectionString()
    {
        return string.Format(_connectionStringFormat, ApplicationConfiguration.GetConnectionString("STARS"));
    }

}

解决方案

There's a constructor overload for DataContext that you can pass a connection string - in that case you can take the setting from anywhere you like.

Edit based on updated question:

I would really like to avoid changing all references to the EF context with an overloaded method including the connection string.

The problem is that the Entities context created by the T4 script generates a const property that is used as connection string

    public const string ConnectionString = "name=FooEntities";

    public FooEntities()
        : base(ConnectionString, ContainerName)
    {
        this.ContextOptions.LazyLoadingEnabled = true;
    }

Since you can't override the default constructor of the partial class, your only other option would be to change the T4 script itself - you should see the following in your .TT script file:

    public <#=code.Escape(container)#>()
        : base(ConnectionString, ContainerName)
    {
<#
        WriteLazyLoadingEnabled(container);
#>
    }

To force your connection string to be used you could modify the constructor call to determine the connection string by calling a static method that you define in a separate file (but for the same partial class FooEntities):

    public <#=code.Escape(container)#>()
        : base(GetCustomConnectionString(), ContainerName)
    {
<#
        WriteLazyLoadingEnabled(container);
#>
    }

Now GetCustomConnectionString() can be defined separately

public partial class FooEntities : ObjectContext
{
   public static string GetCustomConnectionString()
  {
     return "Foobar"; //however you want to determine connection string here
  }
}

You see this is getting complicated and fragile very fast, so I would not advise doing this - but you could.

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

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