EF7 change connectionstring at runtime

后端 未结 1 599
迷失自我
迷失自我 2021-01-07 15:06

In the previous versions of EF we were able to alter the dbcontext connection string as below :

context.Database.Connection.ConnectionString = \"the new conn         


        
相关标签:
1条回答
  • 2021-01-07 15:26

    I found the solution : https://github.com/aspnet/EntityFramework/wiki/Configuring-a-DbContext#config-from-external-code

    Context Code

    public class BloggingContext : DbContext
    {
    public BloggingContext(DbContextOptions options)
        : base(options)
    { }
    
    public DbSet<Blog> Blogs { get; set; }
    }
    

    Application code

    var optionsBuilder = new DbContextOptionsBuilder();
    optionsBuilder.UseSqlServer(@"Server=.\SQLEXPRESS;Database=Blogging;integrated security=True;");
    var context = new BloggingContext(optionsBuilder.Options);
    

    Thank you

    0 讨论(0)
提交回复
热议问题