如何在实体框架6.0中使用多个连接字符串 [英] How to use multiple connection string in entity framework 6.0
问题描述
我使用.net框架和使用实体框架6.0创建了一个Web API。基于代码的迁移已启用。
我有多个连接字符串(假设web.config中存在2个连接字符串),这样如果一个服务器关闭,那么Entity Framework应该使用第二个连接字符串连接到另一个服务器。 / p>
请建议我怎么做到这一点?
公共类DatabaseBootstrapper:IDbContextFactory< TestDBContext>
{
public TestDBContext Create()
{
//我想要一些逻辑来测试服务器是否使用第一个连接字符串,如果服务器不在,那么使用第二连接字符串。
TestDBContext dbcontext = null;
dbcontext = new TestDBContext(@"Data Source = .\sqlexpress3; Initial Catalog = TestDB; User ID = sa; Password = Password $; Trusted_Connection = False;");
返回dbcontext;
}}
公共类TestDBContext:DbContext
{
public DbSet< Nodes>节点{get;组; }
public DbSet< Graphs>图表{get;组; }
public TestDBContext(string connectionString)
:base(connectionString)
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion< TestDBContext,Migrations.Configuration> ());
}
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
您好agrawalamit3101,
我们可以创建一个AppSettings来实现它。我们可以更改appsettings值以连接不同的连接字符串。像这样:
< appSettings>
< add key =" Connstr"值= QUOT;模型2" />
< / appSettings>
< connectionStrings>
< add name =" Model1" connectionString =" data source =(localdb)\ MSSQLLocalDB; initial catalog = EF20161102; integrated security = True; MultipleActiveResultSets = True; App = EntityFramework"的providerName = QUOT; System.Data.SqlClient的" />
< add name =" Model2" connectionString =" data source =(localdb)\ MSSQLLocalDB; initial catalog = Blogging; integrated security = True; MultipleActiveResultSets = True; App = EntityFramework"的providerName = QUOT; System.Data.SqlClient的" />
< / connectionStrings>#Rleated DbContext。
public partial class Model1:DbContext
{
public Model1()
:base(" name =" + ConfigurationManager.AppSettings [" Connstr"]。ToString()+"")
{
}
public virtual DbSet< Blog>博客{get;组; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
}祝你好运,
Cole Wu
I have created a Web API using .net framework and using entity framework 6.0. Code based Migration is enabled.
I have multiple connection strings (suppose 2 connection strings are present in web.config) so that if one server is down then Entity Framework should connect to another server using second connection string.
Please suggest how can I achieve this?
public class DatabaseBootstrapper : IDbContextFactory<TestDBContext> { public TestDBContext Create() { // I want some logic to test server is up or not using first connection string if server is not up then use second connection string. TestDBContext dbcontext = null; dbcontext = new TestDBContext(@"Data Source=.\sqlexpress3;Initial Catalog=TestDB;User ID=sa;Password=Password$;Trusted_Connection=False;"); return dbcontext; }
}
public class TestDBContext : DbContext { public DbSet<Nodes> Nodes { get; set; } public DbSet<Graphs> Graphs { get; set; } public TestDBContext(string connectionString) : base(connectionString) { Database.SetInitializer(new MigrateDatabaseToLatestVersion<TestDBContext, Migrations.Configuration>()); } protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); } }
Hi agrawalamit3101,
We could create a AppSettings to achieve it. we could change the appsettings value to connect different connectionstring. like this:
<appSettings> <add key="Connstr" value="Model2" /> </appSettings> <connectionStrings> <add name="Model1" connectionString="data source=(localdb)\MSSQLLocalDB;initial catalog=EF20161102;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" /> <add name="Model2" connectionString="data source=(localdb)\MSSQLLocalDB;initial catalog=Blogging;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" /> </connectionStrings>#Rleated DbContext.
public partial class Model1 : DbContext { public Model1() : base("name="+ ConfigurationManager.AppSettings["Connstr"].ToString() + "") { } public virtual DbSet<Blog> Blogs { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { } }Best regards,
Cole Wu
这篇关于如何在实体框架6.0中使用多个连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!