✅ EFCore issues when removing an entity,

Hello, so currently im having a bit of issues, im using InMemoryDB. So i have this setup where i have an Item(dynamic data) which user can change, and ItemData(static data). ItemData belongs to some ItemCollection(there are multiple item collections). Now when i combine 2 items, i remove them and create next level which i get from ItemCollection.Items, basically next index from current ItemData. Issue araises that when i combine 2 lvl 1 items everything goes smoothly, but second merge, somehow ItemData.Collection only has 2 items, like lvl 1 item got deleted, but then i created myself debug endpoint to check item collection, and it had 1st item too, so something somewhere is wrong, and i cant wrap my head around it, i am always fetching new data when checking item.
var item1 = await _context.Items.Where(i => i.Id == request.Item1Id).Include(i => i.Data).ThenInclude(d => d.Collection).FirstOrDefaultAsync();

var item2 = await _context.Items.Where(i => i.Id == request.Item2Id).Include(i => i.Data).ThenInclude(d => d.Collection).FirstOrDefaultAsync();
var item1 = await _context.Items.Where(i => i.Id == request.Item1Id).Include(i => i.Data).ThenInclude(d => d.Collection).FirstOrDefaultAsync();

var item2 = await _context.Items.Where(i => i.Id == request.Item2Id).Include(i => i.Data).ThenInclude(d => d.Collection).FirstOrDefaultAsync();
How i remove them
_context.Items.Remove(item1);
_context.Items.Remove(item2);
_context.Items.Remove(item1);
_context.Items.Remove(item2);
Keep in mind, ItemData has not reference to Item as it does not need to have, but Item has navigation to its ItemData, as it is base information about item.
5 Replies
MCUnderground
MCUnderground2mo ago
Update when i have three lvl 1 items, and combine two, the itemdata stays in the collection. so it is probably deleted when i have two of those items only combine them, and then itemdata is not used anywhere, but would it be?
Jimmacle
Jimmacle2mo ago
a good first step is to stop using the in-memory database (assuming you don't mean sqlite in-memory) it's not a proper database and shouldn't really be expected to work like one
MCUnderground
MCUnderground2mo ago
Yeah switched to SQLite real quick same thing. ItemData in database is still there under same collection, but items fetched from context, Their Data.Collection.Items is 2, missing first thing, i do ensure i save changes, in database the given combined items are removed and new one is added but still.
No description
No description
MCUnderground
MCUnderground2mo ago
Hmm so in the same function,
var item2 = await _context.Items.Where(i => i.Id == request.Item2Id).Include(i => i.Data).ThenInclude(d => d.Collection).FirstOrDefaultAsync();
var item2 = await _context.Items.Where(i => i.Id == request.Item2Id).Include(i => i.Data).ThenInclude(d => d.Collection).FirstOrDefaultAsync();
var a = _context.ItemCollections.Include(ic => ic.Items).ToList();
var a = _context.ItemCollections.Include(ic => ic.Items).ToList();
item2.Data.Collection.List has 2 items. a.Items has 3 items. My brain is not braining sorry
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server