I’ve got an old database with a char(1) Status column. I’m using code first and entity framework 4.3.1 to interact with my database. My Status column in my code first class is defined as follows:
[Column(TypeName = "char")]
public string Status { get; set; }
I’m writing a linq query to fetch all items with a Status of one of several values. The code looks something like this (although it’s been simplified):
List<string> statusList = new List<string>() {"N","H","P"};
...
var entries = (from t in context.MyTable where statusList.Conains(t.Status)).ToList();
...
The SQL thats generated prefixes all the Status values with N making the query quite slow.
WHERE ([Extent1].[Status] IN (N'N', N'P', N'P'))
It seems to be because it’s comparing unicode with non unicode so it can’t use the index on the Status column.
I’ve had similar problems before in other linq queries, but I thought they were solved by putting [Column(TypeName = “char”)] on the property.
Does anyone know how I prevent SQL from putting those N’s in front of all my Status values? I tried making statusList a List of char, but then I needed to change the definition of the Status column to char in code first, but that threw errors.
Thanks
David
Are you on .NET Framework 4? I think this was fixed in EF5 core libraries shipped with .NET Framework 4.5. Here is a connect bug for this: http://connect.microsoft.com/VisualStudio/feedback/details/709906/entity-framework-linq-provider-defaulting-to-unicode-when-translating-string-contains-to-like-clause The connect bug also contains a workaround – use EntityFunctions.AsNonUnicode() function to force strings not to be Unicode which may be helpful if you can’t move to .NET Framework 4.5