0

I'm new to Entity Framework and asp.net-mvc 4, I'm getting a Mysql.Data Exception and I couldn't find any related thread on google.

Here's the error:

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

Here's my controller's method:

// GET: api/Partenaires_prestations
[Authorize]
[Route("api/Partenaires_prestations")]
public List<PartenaireMapItem> GetPartenairesWithPrestations()
{
     Random rnd = new Random();

     var queryString = Request.GetQueryNameValuePairs();

     var prestation = queryString.FirstOrDefault();

     return db.Partenaires
         .Where(p => p.PartenairePrestations.Any(pp => pp.Prestation.NomPrestation == prestation.Value))
         .AsEnumerable()
         .Select(p => new PartenaireMapItem {
             IdPartenaire = p.IdPartenaire,
             FirstName = p.FirstName,
             LastName = p.LastName,
             NomComplet = p.LastName.Substring(0,1).ToUpper() + ". " + p.FirstName,
             Type = p.Type,
             DureeMin = 50,
             Lat = p.Lat,
             Lng = p.Lng,
             ImageUrl = p.ImageUrl,
             SeDeplace = p.SeDeplace,
             ADomicile = p.ADomicile,

             Notes = p.NoteClientPartenaires,
             Prestations = new List<string>(p.PartenairePrestations.Select(y => y.Prestation.NomPrestation))
         }).ToList();
}

Thanks to anyone who will take the time to read/answer my post.

11
  • How is your: db variable initialized? Commented Aug 10, 2018 at 10:39
  • 2
    does your connection allow multiple active result sets? Commented Aug 10, 2018 at 10:40
  • @DevilSuichiro how can I see that ? Commented Aug 10, 2018 at 10:40
  • @Stefan private UphairDbEntities db = new UphairDbEntities(); Commented Aug 10, 2018 at 10:42
  • 1
    it should be part of your connection string Commented Aug 10, 2018 at 10:43

1 Answer 1

2

The issue is that you are running multiple queries, on the same connection, simultaneously.

The error is a result of that. You can choose to allow it, or fix it.

Lets fix it (disclaimer; it's a bit of a guess here):

In your code, you have 2 queries actively running on your connection. You can easely fix it by changing the AsEnumerable to ToList

db.Partenaires
         .Where(p => p.PartenairePrestations.Any(pp => pp.Prestation.NomPrestation == prestation.Value))
         //note: the change is here
         .ToList()
         .Select(p => new PartenaireMapItem {
             IdPartenaire = p.IdPartenaire,
             FirstName = p.FirstName,
             LastName = p.LastName,
             NomComplet = p.LastName.Substring(0,1).ToUpper() + ". " + p.FirstName,
             Type = p.Type,
             DureeMin = 50,
             Lat = p.Lat,
             Lng = p.Lng,
             ImageUrl = p.ImageUrl,
             SeDeplace = p.SeDeplace,
             ADomicile = p.ADomicile,

             Notes = p.NoteClientPartenaires,
             Prestations = new List<string>(p.PartenairePrestations.Select(y => y.Prestation.NomPrestation))
         }).ToList();

Be advised, the ToList will force the query to run against the DB directly. The AsEnumerable will deffer execution until it's being enumerated. So the ToList will prevent the "open data reader error" at this specific location.

While enumerating, my guess is, that your code triggers another query, which results in the given error.


As an alternative you could drop the AsEnumerable altogether so a single query will be build. But note, that in that scenario, all functions used in the query must be translated to SQL.


Here's a related one; about a datareader, which is basically the same:

Exception: There is already an open DataReader associated with this Connection which must be closed first

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.