There is already an open DataReader associated with this Command which must be closed first

前端 未结 18 2663
孤街浪徒
孤街浪徒 2020-11-22 01:40

I have this query and I get the error in this function:

var accounts = from account in context.Accounts
               from guranteer in account.Gurantors
           


        
18条回答
  •  生来不讨喜
    2020-11-22 02:26

    I solved this problem by changing await _accountSessionDataModel.SaveChangesAsync(); to _accountSessionDataModel.SaveChanges(); in my Repository class.

     public async Task CreateSession()
        {
            var session = new Session();
    
            _accountSessionDataModel.Sessions.Add(session);
            await _accountSessionDataModel.SaveChangesAsync();
         }
    

    Changed it to:

     public Session CreateSession()
        {
            var session = new Session();
    
            _accountSessionDataModel.Sessions.Add(session);
            _accountSessionDataModel.SaveChanges();
         }
    

    The problem was that I updated the Sessions in the frontend after creating a session (in code), but because SaveChangesAsync happens asynchronously, fetching the sessions caused this error because apparently the SaveChangesAsync operation was not yet ready.

提交回复
热议问题