Access DB context through SignalR Core

前端 未结 3 794
耶瑟儿~
耶瑟儿~ 2021-01-03 12:36

I\'m using AspNetCore.SignalR and I need advice on how to access my SQL Server database through my Hub. There are not a lot of resources out there about this. I know how to

3条回答
  •  不知归路
    2021-01-03 13:21

    I tested this code with .NET Core 3!

    You have to add your DBContext to DI so you can get it from the Constructor of your Hub.

    public class MyHub
    {
          private PlayerContext dbContext;
     
          public MyHub(PlayerContext dbContext)
          {
               this.dbContext = dbContext;
          }
    
          public void YourMethod()
          {
               //call the dbContext here by Dot Notaition
          }
    }
    

提交回复
热议问题