{"id":26373890,"url":"https://github.com/quinntynebrown/effunkiness","last_synced_at":"2025-09-11T17:45:35.291Z","repository":{"id":87500990,"uuid":"321829262","full_name":"QuinntyneBrown/EFFunkiness","owner":"QuinntyneBrown","description":"Examining the behaviour of the enabling retries and the InvalidOperation Exception when executing two queries on the same context at the same time. ","archived":false,"fork":false,"pushed_at":"2020-12-17T02:03:32.000Z","size":34,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-17T01:33:33.461Z","etag":null,"topics":["aspnetcore","efcore5","integration-testing","mediatr","swagger"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/QuinntyneBrown.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2020-12-16T01:01:13.000Z","updated_at":"2020-12-17T02:03:35.000Z","dependencies_parsed_at":null,"dependency_job_id":"655cea81-8368-4d3c-8657-c572a02d467b","html_url":"https://github.com/QuinntyneBrown/EFFunkiness","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/QuinntyneBrown/EFFunkiness","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QuinntyneBrown%2FEFFunkiness","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QuinntyneBrown%2FEFFunkiness/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QuinntyneBrown%2FEFFunkiness/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QuinntyneBrown%2FEFFunkiness/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/QuinntyneBrown","download_url":"https://codeload.github.com/QuinntyneBrown/EFFunkiness/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QuinntyneBrown%2FEFFunkiness/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274681150,"owners_count":25330234,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-11T02:00:13.660Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aspnetcore","efcore5","integration-testing","mediatr","swagger"],"created_at":"2025-03-17T01:29:20.930Z","updated_at":"2025-09-11T17:45:35.271Z","avatar_url":"https://github.com/QuinntyneBrown.png","language":"C#","readme":"# EF Funkiness\n\nAn example of how you can run into a InvalidOperationException stating that \"There is already an open DataReader associated with this Connection which must be closed first.\" using Entity Framework Core. \n\n## To Run the Api (Swagger)\n\nUsing the command line console\n\n1. Navigate to the src\\EFFUnkiness.Server folder\n2. Execute `dotnet run`\n\n## To Run the Integration tests\n\nUsing the command line console\n\n1. Execute `dotnet test`\n\n\n## Funkiness\n\nThe code below will normally throw an InvalidOperationException because while a the underlying DataReader the context is using is open, the Connection is in use exclusively by that DataReader. You cannot execute any commands for the Connection, including creating another DataReader, until the original DataReader is closed.\n\n```csharp\n\n\nvar clients = new List\u003cClientDto\u003e();\n\nvar query = from client in _context.Clients\n            select client;\n\nforeach (var client in query)\n{\n    var user = _context.Users.Single(x =\u003e x.UserId == client.CreatedByUserId);\n\n    clients.Add(new ClientDto(client.ClientId, client.Name, new UserDto(user.UserId, user.Name)));\n}\n\n```\n\n### EnableRetryOnFailure\n\nIf you enabled automatic retries (see snippet below), you will not experience any issues.\n\n```csharp\n\nservices.AddDbContext\u003cEFFunkinessDbContext\u003e(options =\u003e\n{\n    options.UseSqlServer(\"your-connectionstring\",\n        builder =\u003e builder.EnableRetryOnFailure());\n});\n\n```\n\n### ToListAsync\n\nYou could materialize the query and use ToList(). That would be synchronise and not good. Entity Framework gives you an extension called ToListAsync() that seems to be percisely for this situation. It gives you the streaming behaviour and manages the connections.\n\n```csharp\n\nforeach (var client in await query.ToListAsync())\n{\n    var user = _context.Users.Single(x =\u003e x.UserId == client.CreatedByUserId);\n\n    clients.Add(new ClientDto(client.ClientId, client.Name, new UserDto(user.UserId, user.Name)));\n}\n\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquinntynebrown%2Feffunkiness","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fquinntynebrown%2Feffunkiness","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquinntynebrown%2Feffunkiness/lists"}