EF7 在运行时更改连接字符串
EF7 change connectionstring at runtime
在早期版本的 EF 中,我们可以如下更改 dbcontext 连接字符串:
context.Database.Connection.ConnectionString = "the new connectionstring";
我们如何使用 EF7 执行此操作?
谢谢
我找到了解决方案:
https://github.com/aspnet/EntityFramework/wiki/Configuring-a-DbContext#config-from-external-code
上下文代码
public class BloggingContext : DbContext
{
public BloggingContext(DbContextOptions options)
: base(options)
{ }
public DbSet<Blog> Blogs { get; set; }
}
申请代码
var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseSqlServer(@"Server=.\SQLEXPRESS;Database=Blogging;integrated security=True;");
var context = new BloggingContext(optionsBuilder.Options);
谢谢
在早期版本的 EF 中,我们可以如下更改 dbcontext 连接字符串:
context.Database.Connection.ConnectionString = "the new connectionstring";
我们如何使用 EF7 执行此操作?
谢谢
我找到了解决方案: https://github.com/aspnet/EntityFramework/wiki/Configuring-a-DbContext#config-from-external-code
上下文代码
public class BloggingContext : DbContext
{
public BloggingContext(DbContextOptions options)
: base(options)
{ }
public DbSet<Blog> Blogs { get; set; }
}
申请代码
var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseSqlServer(@"Server=.\SQLEXPRESS;Database=Blogging;integrated security=True;");
var context = new BloggingContext(optionsBuilder.Options);
谢谢