ASPNetCoreGraphQL icon indicating copy to clipboard operation
ASPNetCoreGraphQL copied to clipboard

Error deleting an entity after adding it

Open jcsilvers opened this issue 6 years ago • 5 comments

Current build does not allow you to create an entity and then with another mutation delete that entity.

Steps to reproduce:

  1. Create Add Mutation
  2. Create Delete Mutation
  3. Add item
mutation ($day: DayInput!)  {
  createDay(day:$day){
    id
    agendaDay
  }
}

mutation ($day: DayInput!)  {
  createDay(day:$day){
    id
    agendaDay
  }
}

  1. Immediately try and delete
mutation ($dayId: Int!)  {
  deleteDay(dayId:$dayId){
    id
    agendaDay
  }
}

{
  
	"dayId" : "7004"
  
}

I get the following error

{
 "data": {
   "deleteDay": null
 },
 "errors": [
   {
     "message": "There was an error deleting 7004: The instance of entity type 'Day' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.",
     "locations": [
       {
         "line": 2,
         "column": 3
       }
     ],
     "path": [
       "deleteDay"
     ]
   }
 ]
}

I believe this line is the reason

var sp = services.BuildServiceProvider();
services.AddSingleton<ISchema>(new MITSSchema(new FuncDependencyResolver(type =>sp.GetService(type))));

I replaced it with below and it works as expected.

services.AddScoped<IDependencyResolver>(s => new FuncDependencyResolver(s.GetRequiredService));

jcsilvers avatar Dec 10 '18 16:12 jcsilvers

services.AddScoped<IDependencyResolver>(s => new FuncDependencyResolver(s.GetRequiredService));

this generated errors on graphiql for me

omar84 avatar Apr 01 '19 17:04 omar84

Hi @omar84 - do you see the same error @jcsilvers reported? I fixed an issue a couple of weeks ago with the scoping of the repository dependencies that I believe were the root cause of @jcsilvers issue but if you're still seeing it perhaps there is still something lingering.

mmacneil avatar Apr 01 '19 19:04 mmacneil

@mmacneil I didn't try his scenario to be honest, I just thought, if there is an issue, I would want to fix it :)

if you believe its already fixed, then this should probably be closed

omar84 avatar Apr 01 '19 20:04 omar84

ok thanks..so do you get an error (in any scenario) in graphiql?

On Mon, Apr 1, 2019, 5:37 PM Omar M., [email protected] wrote:

@mmacneil https://github.com/mmacneil I didn't try his scenario to be honest, I just thought, if there is an issue, I would want to fix it :)

if you believe its already fixed, then this should probably be closed

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mmacneil/ASPNetCoreGraphQL/issues/2#issuecomment-478737890, or mute the thread https://github.com/notifications/unsubscribe-auth/AAXRu2bgFRD1AnM4wNTyXnT_D4XnzYxiks5vcm4BgaJpZM4ZLtkR .

mmacneil avatar Apr 01 '19 20:04 mmacneil

not with your code, but with what @jcsilvers suggested

omar84 avatar Apr 01 '19 21:04 omar84