I'm kind of new to using EF Core. It was my understanding that EF Core has a "Change Tracker" that looks at the data to see what columns have been modified and builds a SQL UPDATE statement that only updates the modified columns, as described in an answer here:
Does EF core compares value to include columns in update statement?
However, that is not what I am seeing. For example, I have this entity:
public class Book
{
public int BookId { get; set; }
[StringLength(255)]
public string Title { get; set; }
public int AuthorId { get; set; }
public Author Author { get; set; }
[StringLength(500)]
public string Description { get; set; }
}
Then I update an existing row with this code:
class Program
{
static void Main(string[] args)
{
var context = new TestContext();
var book = context.Books.Find(1);
book.Title += " x";
context.Books.Update(book);
context.SaveChanges();
}
}
I started SQL Profiler and ran this code. I expected to see an UPDATE statement that only updated the column "Title", yet it always udpates every column:
exec sp_executesql N'SET NOCOUNT ON;
UPDATE [Books] SET [AuthorId] = @p0, [Description] = @p1, [Title] = @p2
WHERE [BookId] = @p3;
SELECT @@ROWCOUNT;
',N'@p3 int,@p0 int,@p1 nvarchar(500),@p2 nvarchar(255)',@p3=1,@p0=1,@p1=N'',@p2=N'Some Title x'
For example, the column Description was not changed, yet it is in the UPDATE statement, as is AuthorId.
Why is that? Should it not just have Title in the SET clause?