Asp.net 从备用位置获取实体框架连接字符串?
如何从自定义配置文件(而不是web.config)检索Entity Framework 4连接字符串 编辑: 删除默认构造函数生成的代码并在分部类中重新创建以使用拉入的连接字符串是否合理 我确实希望避免使用重载方法(包括连接字符串)更改对EF上下文的所有引用 @这就是我们的结局:Asp.net 从备用位置获取实体框架连接字符串?,asp.net,entity-framework,web-config,Asp.net,Entity Framework,Web Config,如何从自定义配置文件(而不是web.config)检索Entity Framework 4连接字符串 编辑: 删除默认构造函数生成的代码并在分部类中重新创建以使用拉入的连接字符串是否合理 我确实希望避免使用重载方法(包括连接字符串)更改对EF上下文的所有引用 @这就是我们的结局: public partial class STARSEntities { private const string _connectionStringFormat = @"metadata=res://*/ST
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"));
}
}
公共部分类起始点
{
private const string | u connectionStringFormat=@“metadata=res://*/STARS.EntityModel.STARSModel.csdl://*/STARS.EntityModel.STARSModel.ssdl | res://*/STARS.EntityModel.STARSModel.msl;provider=System.Data.SqlClient;provider连接字符串='Data Source={0};MultipleActiveResultSets=True';
///
///使用STARS.xml配置文件中的连接字符串初始化新的StarSenties对象。
///
///
///如果从数据库重新生成StarSenties类,则需要从生成的文件中删除默认构造函数。
///
public starsenties():base(GetConnectionString(),“starsenties”)
{
this.ContextOptions.LazyLoadingEnabled=true;
OnContextCreated();
}
私有静态字符串GetConnectionString()
{
返回string.Format(_connectionStringFormat,ApplicationConfiguration.GetConnectionString(“STARS”));
}
}
有一个用于DataContext
的构造函数重载,您可以传递一个连接字符串-在这种情况下,您可以从任意位置获取设置
根据更新的问题进行编辑:
我真的想避免改变
对EF上下文的所有引用
一种重载方法,包括
连接字符串
问题在于T4脚本创建的Entities上下文生成了一个用作连接字符串的const属性
public const string ConnectionString = "name=FooEntities";
public FooEntities()
: base(ConnectionString, ContainerName)
{
this.ContextOptions.LazyLoadingEnabled = true;
}
由于无法重写分部类的默认构造函数,因此唯一的其他选项是更改T4脚本本身-您应该在.TT脚本文件中看到以下内容:
public <#=code.Escape(container)#>()
: base(ConnectionString, ContainerName)
{
<#
WriteLazyLoadingEnabled(container);
#>
}
现在可以单独定义GetCustomConnectionString()
public partial class FooEntities : ObjectContext
{
public static string GetCustomConnectionString()
{
return "Foobar"; //however you want to determine connection string here
}
}
您可以看到,这变得非常复杂和脆弱,因此我不建议您这样做,但您可以这样做。您能够从这个自定义配置文件中读取连接字符串吗?如果是这样,您可以使用获取ConnectionString的
NorthWindDataContext nwdc = new NorthWindDataContext(alternateConnectionString);
也许是这样的
// replace the following line with any business logic you need to get to the file
// with your connection string. If it is a config-style file, you can use the
// Frameworks's helper classes as well
string connectionString= File.ReadAllText("alternative path");
Entities ent = new Entity(connectionString);
下面是一个解释。不知道这是否是您要求的,但您可以使用ConnectionString元素的“configSource”属性:
<connectionStrings configSource="connection.config">
</connectionStrings>
您可以使用
EntityConnectionStringBuilder
:
var ecb = new EntityConnectionStringBuilder();
ecb.Metadata = "res://*/Model.MyModel.csdl|res://*/Model.MyModel.ssdl|res://*/Model.MyModel.msl";
ecb.Provider = "System.Data.SqlClient";
ecb.ProviderConnectionString = connectionStringFromFancySource;
return new MyModel(ecb.ToString());
您现在正在使用什么EF上下文构造函数?您必须以某种方式传递配置,这是不可避免的,现在我们正在使用默认构造函数,它引用web.config中的连接字符串。我没有直接使用这个方法,但它工作正常。我很懒,不想处理域服务如何从实体模型获取连接字符串的问题,所以重写默认EF构造函数以使用引用外部构造函数的连接字符串。@Noel:刚刚更新了答案-这很复杂,但似乎可能是您试图做的事-请注意当您通过运行T4脚本重新生成实体时,更改的构造函数调用将被覆盖-您必须在每次更新实体时手动进行更改。
var ecb = new EntityConnectionStringBuilder();
ecb.Metadata = "res://*/Model.MyModel.csdl|res://*/Model.MyModel.ssdl|res://*/Model.MyModel.msl";
ecb.Provider = "System.Data.SqlClient";
ecb.ProviderConnectionString = connectionStringFromFancySource;
return new MyModel(ecb.ToString());