var row = DataContext.TableA.FirstOrDefault(x => x.Col1 == "foo" && x.Col2 == "bar");
row.Col3 = "xyz";
DataContext.SaveChanges()
I expected Col3="xyz" to be updated only for Col1="foo" and Col2="bar". But it updated more rows that I expected. When I logged the queries, EF generated query seems to be missing Col2 altogether. Any idea why?
UPDATE [dbo].[TableA]
SET [Col3] = @0
WHERE ([Col1] = @1)
-- @0: 'xyz' (Type = String)
-- @1: 'foo' (Type = String)
When using Fluent API to specify composite primary key you must use new keyword to create anonymous type.
HasKey(m => new { m.Col1, m.Col2 });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With