EF7 在运行时更改连接字符串 [英] EF7 change connectionstring at runtime
本文介绍了EF7 在运行时更改连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在以前版本的 EF 中,我们可以更改 dbcontext 连接字符串,如下所示:
In the previous versions of EF we were able to alter the dbcontext connection string as below :
context.Database.Connection.ConnectionString = "the new connectionstring";
我们如何使用 EF7 做到这一点?
How can we do this with 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);
谢谢
这篇关于EF7 在运行时更改连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文