C
C#3y ago
kopuo

DbContext.SaveChangesAsync has no effect on the collection of changed objects

Hi. I get products collection from dbContext, change some values and want to save it in the database, but after SaveChangesAsync () is done in the database there are no changes. When previewing objects from dbContext at the end, I also cannot see the edited fields:
var products = await _dbContext.Products.AsEnumerableAsync()

foreach(var p in products)
{
if(p.Price == 0)
p.Price = 10
}

await _dbContext.AddRangeAsync(products);

await _dbContext.SaveChangesAsync();
var products = await _dbContext.Products.AsEnumerableAsync()

foreach(var p in products)
{
if(p.Price == 0)
p.Price = 10
}

await _dbContext.AddRangeAsync(products);

await _dbContext.SaveChangesAsync();
Product is an entity btw.
5 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
kopuo
kopuoOP3y ago
Without it, it also does'nt save changes to the database
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Mayor McCheese
EF tracks this for you
kopuo
kopuoOP3y ago
Ok, on the List works
Want results from more Discord servers?
Add your server