2016-05-12 81 views
0

我正在玩弄ASP.NET 5身份識別並陷入困境。ASP.NET身份框架無法正常工作的實體

這是通過身份樣板背景下創造的:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{ 
    public DbSet<Transaction> Transactions { get; set; } 

    protected override void OnModelCreating(ModelBuilder builder) 
    { 
     base.OnModelCreating(builder); 
    } 
} 

我添加了存在身份框架之外的附加實體Transactions

當我啓動該網站,所有驗證的東西工作正常,但是當我嘗試查詢Transactions我得到這個錯誤:

InvalidOperationException: No database providers are configured. Configure a database provider by overriding OnConfiguring in your DbContext class or in the AddDbContext method when setting up services.

進一步尋找這個錯誤,所有的跡象似乎都指向註冊Startup.cs中的服務:

public void ConfigureServices(IServiceCollection services) 
{ 
    services.AddEntityFramework() 
     .AddSqlServer() 
     .AddDbContext<ApplicationDbContext>(options => 
      options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])); 

    services.AddMvc(); 
} 

儘管如此,仍然出現錯誤。

謝謝!

回答

0

我知道這是愚蠢的,找到了答案。

當使用ef上下文時,當它是新派生的,上下文沒有設置。

所以它需要注入:

public class HomeController : Controller 
{ 
    private readonly ApplicationDbContext _appCtx; 

    public HomeController(
     ApplicationDbContext appCtx) 
    { 
     _appCtx = appCtx; 
    } 

    public JsonResult Durr() 
    { 
     return new JsonResult(_appCtx.Users.ToList());  
    } 
} 
相關問題