Entity Framework varchar foreign key case insensitive

The following question was asked a few years ago: Entity Framework nvarchar Case-sensitive foreign key .

In short, the answer was: EF uses the CLR to compare keys for lazy-loaded associations and always makes it case-sensitive, even if the database is set up for case-insensitive sorting.

Unfortunately, the project I am currently involved in uses the VARCHAR primary key columns. Database mapping is case insensitive.

Committing a database design is not really an option, other than possibly setting up CS mapping in primary key columns (but this could potentially break client applications).

So my question is twofold:

  • Does Entity Framework provide a directive or setting of some kind to enable it to make case insensitive comparisons?
  • If not, can the trigger be used to automatically change the foreign key to match the primary key case? Or can you think of any other workarounds?

BTW: SQL Server 2008 R2 and Entity Framework version 6.

+5
source share
1 answer

without much analysis, here is a list of things you can try:

  • Write down the stored procedures and use the LOWER () function, here is the link: http://www.w3schools.com/sql/sql_func_lcase.asp
  • Use the first model to map the database
  • Get both objects using ModelContext. Entity'.Find ('PrimaryKey') and make an โ€œassociationโ€ using the toLowerCase () method in .NET.
  • Make a view in SQL Server if you are trying to get data without changing the case
+1
source

All Articles