-->

How to make Entity Framework DbContext generic for

2019-07-30 12:07发布

问题:

This is my method in the business layer:

public async task<model> GetMemberList(CancellationToken cancelToken, string connString){
    try
    {
        await Task.Run(() =>
        {
             using (var dbContext = DbContext.Create(connString))
                {
                   Code Goes Here....
                }
        }, cancelToken);
      }
      catch
      {
         Throw New Exception(); 
      }
    }

we have different clients databases. we pass connString from mvc controller to business layer methods to initialize new instance of dbContext to connect relevant client database. You can see in my method we used DbContext.Create(connString) to create a connection with database always when this methods run. My problem is I can't use moq Framework for unit testing because of this situation. Is there a way to generalize this for dynamic connection string and move dbContext initialization outside the method?