2016-02-26 17 views
1

我有这样的代码:OnConfiguring为的DbContext - 代码没有运行 - ASP.NET 5

public class TestDbContext : DbContext 
{ 
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
    { 
     if (!optionsBuilder.IsConfigured) 
     { 
      optionsBuilder.UseSqlServer(@"Server=(local);Database=MyDb;Trusted_Connection=True;"); 
     } 
    } 
} 

根据描述,这将在TestDbContext的新实例被解雇。我有一个创建一个新的上下文代码:

TestDbContext ctx = new TestDbContext(); 

我期待OnConfiguring将被解雇,所以我可以设置我的连接字符串,但代码不经过这里(我有断点)。我需要其他什么设置。

+0

什么是你正在使用EF的版本? – Vasanthan

+0

@Vasanth:使用EF 7 –

回答

0

我的DI导致OnConfiguring(..)也没有解雇。

对于我来说,我是能够增加一个构造函数来解决问题

protected MyDbContext(DbContextOptions options) : base(options) 
{ 

} 

,或者如果你更喜欢..

protected MyDbContext() : base(new DbContextOptions<MyDbContext>()) 
{ 

}