如何在Facade设计模式中使用ApplicationDbContext?

时间:2018-06-11 19:28:47

标签: c# design-patterns

我正在.NET Core使用Razor页面和MVVM模型进行Web应用程序,我实现了外观设计模式 - 至少是尝试过。它还没有用。

ViewModel.cshtml.cs

[ValidateAntiForgeryToken]
public async Task<IActionResult> OnPostAsync()
{

     ...

     Facade facade = new Facade();
     facade.Execute()
     ...

     return RedirectToPage();
}

Facade.cs

public class Facade
{
   private SubSystem _subSystem;

   public Facade()
   {
       _subSystem = new SubSystem(_dbC);
   }

   public void Execute()
   {
       _subSystem.Execute();
   }
}

SubSystem.cs

ApplicationDbContext _dbContext = new ApplicationDbContext(new DbContextOptions<ApplicationDbContext>());

public void Execute()
{
    bool exists = _dbContext.Table.Any(x => x.Name == .. ); // <!-- Crashes here
    ...
}

每当我到达Execute() - 方法本身时,它会崩溃并向我显示此错误消息:

InvalidOperationException: No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.

我知道其他帖子已经讨论过此错误消息,但我没有找到关于此模式的消息。我在这里错过了什么?

0 个答案:

没有答案