Sql Server 2016, Entity Framework and .net 4.5.1

Ran into an interesting bug today.  I’m working with a client that’s using .net 4.5.1, EF 6.1.3, and is in the process of upgrading to SQL Server 2016.

When we did the update, we suddenly had entities that were being detected as being different, even though the data was exactly the same.  This was causing us to get unexpected concurrency exceptions.

The following sql would be generated by entity framework:

exec sp_executesql N'UPDATE [dbo].[MyTable]
SET [StateLastChangedDate] = @0
WHERE (([Id] = @1) AND ([LastEdited] = @2))
SELECT [LastEdited]
FROM [dbo].[MyTable]
WHERE @@ROWCOUNT > 0 AND [Id] = @1',N'@0 datetime2(7),@1 uniqueidentifier,@2 binary(8)',@0='2016-08-04 17:28:17.2366667',@1='FC66D162-1801-409C-8574-4B1900C72B2E',@2=0x0000000000004D3B

StateLastChangedDate would be identical to what it was previously.  Same ticks and everything.

We found two solutions:

1.  Change the compatibility mode to Sql Server 2014

2.  Upgrade to .net 4.6.2

Both seemed to eliminate the issue with a field being detected as being changed when it shouldn’t have been.

This article is part of the GWB Archives. Original Author: Robert May

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.