thiccfire
thiccfire

Reputation: 31

Azure Function Timeout with Entity Framework

I am trying to create a basic timer function that writes data to a database using Entity Framework, however I get the error:

System.Data.SqlClient: There is already an open DataReader associated with this Command which must be closed first.

This error occurs when I call context.SaveChanges();

Upvotes: 0

Views: 322

Answers (1)

Mohit Verma
Mohit Verma

Reputation: 5294

@thiccfire

Entity Framework only supports one active command per context at a time.

This can happen if you execute a query while you are still iterating over the result set from another query. Though you have not provide the code example so It is not clear from where this happens.

One thing that can cause this is lazy loading triggered when iterating over the results of some query.

This can be easily solved by allowing MultipleActiveResultSets=true in your connection string. Add to the provider part of your connection string (where Data Source, Initial Catalog, etc. are specified).

Another way is that you can materialize your first query in your code by doing .ToList() and then you can use it for further processing.

Hope it helps.

Upvotes: 1

Related Questions