A couple of years ago the following question was asked: Entity Framework nvarchar Case Sensitivity on Foreign key.
In short the answer was: EF uses the CLR to compare the keys of lazily loaded associations and always does that in a case sensitive manner, even though the database is set to a case insensitive collation.
Unfortunately, the project I'm currently involved with heavily uses VARCHAR primary key columns. The database collation is case insensitive.
Fixing the database design is really not an option, besides perhaps setting a CS collation on the primary key columns (but that would potentially break client applications).
So my question is two-fold:
BTW: SQL Server 2008 R2 and Entity Framework version 6.
without much analysis, here's a list of thing you can try:
Now in EF Core it is possible to circumvent this problem by using a ValueComparer
. See the following github issue comment: https://github.com/dotnet/EntityFramework.Docs/issues/2979#issuecomment-753344003
Docs: https://learn.microsoft.com/en-us/ef/core/modeling/value-comparers?tabs=ef5#key-comparers
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