-
Notifications
You must be signed in to change notification settings - Fork 162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using EF-Core with FieldAsync and dep injection #204
Comments
This should not be a problem, DI should work with async fields if async / await are used correctly. We have this in development currently, but it seems to be working fine with queries and mutations. Are you injecting the DbContext into the classes that extend ObjectGraphType? We do it via the userContext (insert the ServiceProvider from the httpContext into the user context class in the userContextBuilder) and then call 'Services.GetService()'. |
Hey I was directly injecting it into the query like this: public RootQuery(PanelContext panelContext)
{
// User and Users
FieldAsync<UserType>(
"user",
arguments: new QueryArguments(
new QueryArgument<IdGraphType> {Name = "id", Description = "The ID of the user."}),
resolve: async context =>
{
var id = context.GetArgument<int>("id");
var user = await panelContext.Users.AsNoTracking()
.FirstOrDefaultAsync(x => x.Id == id);
return user;
});
FieldAsync<ListGraphType<UserType>>(
"users",
resolve: async context => await panelContext.Users.AsNoTracking().ToListAsync()
);
} When I access it through the service as you mentioned it seems to work without any issues. |
Injecting it into the query probably will not work indeed, as it is resolved by the DI as a Singleton and the DbContext Scoped (I hope you do not use a Singleton for a DbContext). |
You can check this issue: |
Yes, @S0PEX you can find a comprehensive answer for your question in graphql-dotnet/graphql-dotnet#648 |
Hey,
we are currently working with GrapQL and Asp.Net Core at our University. And I ran into an issue when I dependency inject the DatabaseContext into my query and then use async fields. When I try to query something ef core throws an exception that the current connection is already used. My guess would be that the fields are evaluated at the same time and they all share the same injected ef context.
Changing the fields to non-async ones fixes the issue as expected.
I am aware that this issue is probably related to the internal workings of the EF but I was wondering if somebody experienced the same issue was able to fix this behavior. Or if somebody has a clue how to fix it.
Regards Artur
The text was updated successfully, but these errors were encountered: