How to make Entity Framework DbContext generic for dynamic connection string?

谁说胖子不能爱 提交于 2019-12-11 06:55:35

问题


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?

来源:https://stackoverflow.com/questions/51870821/how-to-make-entity-framework-dbcontext-generic-for-dynamic-connection-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!