Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EntityFramework DBContext FirstOrDefault关闭连接_C#_Asp.net Mvc_Entity Framework_Asp.net Mvc 4_Dependency Injection - Fatal编程技术网

C# EntityFramework DBContext FirstOrDefault关闭连接

C# EntityFramework DBContext FirstOrDefault关闭连接,c#,asp.net-mvc,entity-framework,asp.net-mvc-4,dependency-injection,C#,Asp.net Mvc,Entity Framework,Asp.net Mvc 4,Dependency Injection,我有点像一个JUnito程序员,所以我需要理解这一点。我正在使用EntityFramework 5。我们实现了一个存储库类,它继承了DbContext。调用FirstOrDefault方法时,如果没有要返回的值,EntityFramework将关闭连接。这会使代码停止,并抱怨dbContext.Database.Connection.ServerVersion为null。事实上,连接状态为“关闭”。我实际上是注入存储库来测试服务,而不使用Ninject之类的工具。这就是发生这种情况的原因吗 这是

我有点像一个JUnito程序员,所以我需要理解这一点。我正在使用EntityFramework 5。我们实现了一个存储库类,它继承了DbContext。调用FirstOrDefault方法时,如果没有要返回的值,EntityFramework将关闭连接。这会使代码停止,并抱怨dbContext.Database.Connection.ServerVersion为null。事实上,连接状态为“关闭”。我实际上是注入存储库来测试服务,而不使用Ninject之类的工具。这就是发生这种情况的原因吗

这是我注入存储库的地方:

IRepository<stock_queues> repo = new DbContextRepositoryEcom<stock_queues>(new DbContextFactory<eComEntities>(new eComEntities()));

        StockFlashService service = new StockFlashService(repo, new PartsService(), new UserService());

        service.AddToStockFlashQueue("asfsdfgvsdf");
IRepository repo=new DbContextRepositoryEcom(new DbContextFactory(new eComEntities());
StockFlashService服务=新的StockFlashService(repo、new PartsService()、new UserService());
AddToStockFlashQueue(“asfsdfgvsdf”);
这就是我实际运行代码的地方(在不同的项目中):

public StockFlashService(IRepository队列、IPartsService部件、IUserService用户)
{
这。_QueuesRepo=队列;
本._零件服务=零件;
_UsersService=用户;
//userId=\u UsersService.GetUser().userId;
userId=30;//仅用于测试!!
}
公共无效AddToStockFlashQueue(字符串键)
{            
库存队列sq=新库存队列();
sq.零件号=钥匙;
sq.userID=用户id;
sq.timestamp=DateTime.Now;
//将新条目添加到库存闪存队列(如果需要)
库存部分;
int count=_QueuesRepo.count();
如果(_QueuesRepo.Count()>0)
{               
part=\u QueuesRepo.FirstOrDefault(f=>f.userID==sq.userID&&f.partnumber==sq.partnumber);//这就是全部
}
}

我正在使用一个控制台应用程序来测试服务并注入依赖项。该服务所承载的项目(不是windows/web服务,但实际上是发挥所有作用的逻辑)是一个MVC应用程序。

FirstorDefault在没有值时返回null,就像我们的情况一样。首先检查null,然后将其分配给“零件”

  public StockFlashService(IRepository<stock_queues> queues, IPartsService parts, IUserService users)                     
    {
        this._QueuesRepo = queues;          
        this._PartsService = parts;
        _UsersService = users;
      //  userId = _UsersService.GetUser().UserId;
        userId = 30; //FOR TESTING ONLY !!
    }

 public void AddToStockFlashQueue(string key)
    {            
        stock_queues sq = new stock_queues();
        sq.partnumber = key;
        sq.userID = userId;
        sq.timestamp = DateTime.Now;

        // Adds new entry to Stock Flash queue (if required)
        stock_queues part;
        int count = _QueuesRepo.Count();
        if (_QueuesRepo.Count() > 0)
        {               
            part = _QueuesRepo.FirstOrDefault(f => f.userID == sq.userID && f.partnumber == sq.partnumber); // THIS IS WERE IT ALL BRAKES

        }
}