C
C#2w ago
Ancient77

Entity framework overwriting List works?

I always thought that just overwriting a List like solution.Skus = []; doesnt work since the change tracker doesnt update everything, but after some testing it seems to work. Why does everyone then always says to not do this?
var sku1 = new Sku
{
SkuCode = "Sku1",
Name = "Sku1",
BusinessCode = "BusinessCode1"
};
_newDb.Context.Solutions.Add(new Data.Models.Solution
{
Name = "1",
Skus = [sku1]
});
_newDb.Context.Solutions.Add(new Data.Models.Solution
{
Name = "2",
Skus = [sku1]
});
await _newDb.Context.SaveChangesAsync();
_newDb.Context.ChangeTracker.Clear();
var test1 = await _newDb.Context.Solutions.Include(x => x.Skus).FirstOrDefaultAsync();
Assert.That(test1.Name, Is.EqualTo("1"));
Assert.That(test1.Skus.Single().Name, Is.EqualTo("Sku1"));
test1.Skus = [new Sku
{
SkuCode = "Sku2",
Name = "Sku2",
BusinessCode = "BusinessCode2"
}];
await _newDb.Context.SaveChangesAsync();
_newDb.Context.ChangeTracker.Clear();
var test2 = await _newDb.Context.Solutions.Include(x => x.Skus).ToListAsync();
Assert.That(test2.Single(x => x.Name == "1").Skus.Single().Name, Is.EqualTo("Sku2"));
var sku1 = new Sku
{
SkuCode = "Sku1",
Name = "Sku1",
BusinessCode = "BusinessCode1"
};
_newDb.Context.Solutions.Add(new Data.Models.Solution
{
Name = "1",
Skus = [sku1]
});
_newDb.Context.Solutions.Add(new Data.Models.Solution
{
Name = "2",
Skus = [sku1]
});
await _newDb.Context.SaveChangesAsync();
_newDb.Context.ChangeTracker.Clear();
var test1 = await _newDb.Context.Solutions.Include(x => x.Skus).FirstOrDefaultAsync();
Assert.That(test1.Name, Is.EqualTo("1"));
Assert.That(test1.Skus.Single().Name, Is.EqualTo("Sku1"));
test1.Skus = [new Sku
{
SkuCode = "Sku2",
Name = "Sku2",
BusinessCode = "BusinessCode2"
}];
await _newDb.Context.SaveChangesAsync();
_newDb.Context.ChangeTracker.Clear();
var test2 = await _newDb.Context.Solutions.Include(x => x.Skus).ToListAsync();
Assert.That(test2.Single(x => x.Name == "1").Skus.Single().Name, Is.EqualTo("Sku2"));
13 Replies
wasabi
wasabi2w ago
Because the old list instance may be held elsewhere.
Ancient77
Ancient77OP2w ago
@wasabi What do you mean by held elsewhere? this is the only code im testing and I tried to clear the changetracker
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Ancient77
Ancient77OP7d ago
ah so I CAN use ef core like that? I always thought / heared that this is not good for ef core
Unknown User
Unknown User7d ago
Message Not Public
Sign In & Join Server To View
Ancient77
Ancient77OP7d ago
yea but the problem is its working, when it would expect it to not work, so not really sure how I can find out why that is
Unknown User
Unknown User7d ago
Message Not Public
Sign In & Join Server To View
Ancient77
Ancient77OP7d ago
it was just a quick example to show what is happening. I just wanted to clear the changetracker to make sure its not some weird cached state behavior
Unknown User
Unknown User7d ago
Message Not Public
Sign In & Join Server To View
Ancient77
Ancient77OP7d ago
I tried to do it with the asserts, but basically everything is working, but I thought you couldn't just replace a list in ef core. In the example I am replacing the test1.Skus with a completely new list, and everything works (the old skus are getting disconnected, and new ones connected). But all the guides always said to not do this, but to use .Add() .Remove() .Clear()
Unknown User
Unknown User7d ago
Message Not Public
Sign In & Join Server To View
blinkbat
blinkbat7d ago
state tracking is more trouble than it's worth IMO just assemble your dtos and fire the needed queries
Unknown User
Unknown User7d ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?