Reputation: 13945
Given this table:
I need to update the Rate and Average for all 4 rows.
var currency = Context.Set<Currency>().Select(c => c);
That will create currency
as an iQueryable. But now I'm having trouble envisioning how to formulate the update statement to update Rate
and Average
in all four rows.
What I need to do is the equivalent of this:
update Currencies set rate = 1.7, Average = 1.1 where CurrencyId = 1
update Currencies set rate = 2.5, Average = 2.1 where CurrencyId = 2
update Currencies set rate = 3.1, Average = 3.1 where CurrencyId = 3
update Currencies set rate = 42.7, Average = 42.1 where CurrencyId = 4
Upvotes: 3
Views: 7894
Reputation: 932
Maybe you can just use IEnumerable.
var currencies = Context.Set<Currency>().Select(c => c);
// var currencies = Context.Set<Currency>()Where(c => somecondition(c));
foreach(var c in currencies.ToList())
{
c.Rate = ComputeRate(c);
c.Average = ComputeAverage(c);
}
Context.SaveChanges();
Upvotes: 2
Reputation: 6430
Simple approach would be:
var currency1 = Context.Currency.Find(1);
curreny1.Rate = 1.7;
currency1.Average = 1.1;
//do the same for other currencies and finally save changes
Context.SaveChanges();
In case you want to avoid additional GET call to database to get the currency object using id, then
var currency1 = new Currency{CurrenyId = 1, Rate = 1.7, Average = 1.1 };
var entry1 = Context.Entry(currency1);
entry1.Property(p=> p.Rate).IsModified = true;
entry1.Property(p=> p.Average).IsModified = true;
Context.SaveChanges();
Upvotes: 2