I have a little problem saving data in the database. In a given method, I make a request to an API, serialize the data, and try to save to the database, as shown in the image, but I end up getting an error referring to the application's DataContext, saying:
System.InvalidOperationException: The instance of entity type 'Launch ' 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.
Any tips on how to resolve this issue?
CodePudding user response:
Maybe your list contains duplicates. EF does not allow to track entites with same key. If you have that error, try
_context.Entry(<entity>).State = EntityState.Detached;
CodePudding user response:
I can see in your Article class
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int id { get; set; }
Try to change it
[Key]
public int id { get; set; }