Umbraco UI Builder DB Repository SaveImpl errors
# help-with-umbraco
d
hi all. Im having issues with my implementation of UI Builder which uses 2 tables. so a one to many relationship. Im using repository aswell for the listview and detailview. however when using the SaveImpl method to save the UI builder item I get this error: "The instance of entity type 'xxxxxxxx' cannot be tracked because another instance with the key value '{Id: 105}' is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached." I've used Entity framework as orm and I pretty much used the default way of attaching it: builder.Services.AddUmbracoDbContext((serviceProvider, options) => { options.EnableSensitiveDataLogging(); options.UseUmbracoDatabaseProvider(serviceProvider); }); can anyone give me any pointers to where i might be going wrong with this?
j
What are you doing in your saveimpl method?
d
protected override PatternCatalogueRowUniqueDTO SaveImpl(PatternCatalogueRowUniqueDTO entity) { var currentExists = _context.PatternCatalogueRowUniques?.Any(i => i.Id == entity.Id); if (currentExists == true) { _context.PatternCatalogueRowUniques?.Update(entity); } else { _context.PatternCatalogueRowUniques?.Add(entity); } _context.SaveChanges(); return entity; }
bare in mind that i've tried a lot of stuff, so this might look ugly.
Doing this _context.ChangeTracker.Clear(); seemed to have fixd it - however I'm reaaaaally not sure thats the right way to go. If anyone has any objections on doing that Im all ears.
well - ill mark this as solved. for some reason after I ran the clear function i could remove the functionality and it worked out of the box as expected.
7 Views