EF7在运行时更改连接字符串 [英] EF7 change connectionstring at runtime

查看:190
本文介绍了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 -外部代码

上下文代码

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆