Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF missing where clause in update

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)
like image 224
Nemo Avatar asked Jul 24 '26 10:07

Nemo


1 Answers

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 });
like image 67
Adil Mammadov Avatar answered Jul 27 '26 03:07

Adil Mammadov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!