在 EF 的早期版本中,我们能够更改 dbcontext 连接字符串,如下所示:
context.Database.Connection.ConnectionString = "the new connectionstring";
我们如何使用 EF7 做到这一点?
谢谢
我找到了解决方案:https://github.com/aspnet/EntityFramework/wiki/Configuring-a-DbContext#config-from-external-code 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);
谢谢
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)