从备用位置获取实体框架连接字符串?
如何从自定义配置文件而不是 web.config 检索 Entity Framework 4 连接字符串?
编辑: 删除默认构造函数生成的代码并在分部类中重新创建它以使用拉入的连接字符串是否合理?
我真的很想避免使用包括连接字符串在内的重载方法更改对 EF 上下文的所有引用。
@BrokenGlass:这就是我们最终得到的结果:
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"));
}
}
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"));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
DataContext
有一个构造函数重载,您可以传递连接字符串 - 在这种情况下,您可以从任何您喜欢的地方获取设置。根据更新的问题进行编辑:
问题是 T4 脚本创建的实体上下文生成一个用作连接字符串的 const 属性,
因为您无法覆盖分部类的默认构造函数,所以您唯一的其他选择就是更改 T4 脚本本身 - 您应该在您的 .TT 脚本文件中看到以下内容:
要强制使用连接字符串,您可以修改构造函数调用,通过调用您在单独文件中定义的静态方法来确定连接字符串(但对于相同的分部类 < code>FooEntities):
现在可以单独定义
GetCustomConnectionString()
您会发现这很快变得复杂且脆弱,所以我不建议这样做 - 但您可以。
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:
The problem is that the Entities context created by the T4 script generates a const property that is used as connection string
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:
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
):Now
GetCustomConnectionString()
can be defined separatelyYou see this is getting complicated and fragile very fast, so I would not advise doing this - but you could.
不知道这是否是您所要求的,但您可以使用connectionStrings元素的“configSource”属性:
Don't know if this is what you ask for, but you can use the "configSource" attribute of the connectionStrings element:
也许是这样的?
这是Microsoft Connect 上的帖子< /a> 解释了这一点。
Something like this perhaps?
Here's a post on Microsoft Connect that explains this.
您能够从此自定义配置文件中读取连接字符串吗?如果是这样,您可以使用采用 ConnectionString 的 DataContext 构造函数。
Are you able to read the connection string from this custom config file? if so, you can use the constructor for your DataContext that takes ConnectionString.
您可以使用EntityConnectionStringBuilder:
You can use
EntityConnectionStringBuilder
: