Sto lavorando con il core del framework di entità in memoria per testare il mio codice. Ho creato nel contesto di memoria e aggiunto dati. Ho un metodo di eliminazione che sta per eliminare il record con id = 1. Il metodo di prova genera l'errore dopo l'errore:
L'istanza del tipo di entità 'gatti' non può essere tracciata perché un'altra istanza con il valore chiave '{Id: 204}' è già tracciata Quando si collegano entità esistenti, assicurarsi che sia collegata solo un'istanza di entità con un determinato valore chiave.
Qualcuno potrebbe aiutarmi, come risolvere questo problema. Ho provato diversi scenari su Internet, ma nulla non ha funzionato.
Test.cs:
[Fact(DisplayName ="Delete Service Must Return Result")]
public void DeleteService()
{
var serviceId = _collectionFixture.context.cat.Where(x => x.Id == 201).AsNoTracking();
var okObject = _collectionFixture.CatController.DeleteService(serviceId) as OkObjectResult;
var baseResponse = okObject.Value as BaseResponse;
Assert.True(baseResponse.Success);
}
la mia classe di servizio:
public catResponse DeleteService (int serviceId) {provare {
var serviceDto = _Context.catserviceTreatment.Where(x => x.Id
==serviceId).AsNoTracking().firstordefault();
if(serviceDto!=null)
{
this._Context.catserviceTreatment.Remove(serviceDto);
this._Context.SaveChanges();
return new catResponse { Success = true, Error = "service
deleted successfully" };
}
}catch(exception e)
{
}
}
il mio apparecchio da collezione:
public catContext GetDbContext()
{
var options = new DbContextOptionsBuilder<catContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking)
.ConfigureWarnings(x =>
x.Ignore(InMemoryEventId.TransactionIgnoredWarning))
// .ConfigureWarnings(x=>x.Ignore(InMemoryEventId.))
.EnableSensitiveDataLogging()
.Options;
var context = new CatDirectoryContext(options);
var catCategory = new List<catcategory>()
{
new catCategory{Id=100,CategoryName="Youth
teereafjkd",UpdatedBy="Test",UpdatedWhen=DateTime.Now},
new catCategory{Id=101,CategoryName="Adult
fdsafd",UpdatedWhen=DateTime.Now},
};
context.catcategory.AddRange(catCategory);
context.SaveChanges();
var catService = new List<catserviceTreatment>()
{
new catserviceTreatment{Id=200,Name="House of
Hope",Company="",
Address ="2001 st street",City="alone
city",State="UT",ZipCode=8404,Category=null,
CategoryId =100,Description="this is
description",UpdatedBy="test",UpdatedWhen=DateTime.Now},
new catserviceTreatment{Id=201,Name="Odyssey
House",Company="",
Address ="2001 st",City="bare
city",State="UT",ZipCode=84dfd,Category=null,
CategoryId =101,Description="this is d
description",UpdatedBy="test",UpdatedWhen=DateTime.Now},
}
context.catserviceTreatment.AddRange(catService);
context.SaveChanges();
return context;
}
Ho avuto lo stesso errore. Durante l'installazione ho aggiunto alcuni record di test che iniziano con ID 1. Sembra che anche il database in memoria inizi a numerare da 1 quando vengono aggiunti nuovi record, causando l'errore "entità già tracciata".
Dopo aver modificato il mio codice di inizializzazione per utilizzare gli ID a partire da 101, il problema è stato risolto.