0

I am very new to EF so my descriptions may not make sense. Please ask me to clarify anything and I'll do my best to find the answer. In an existing application we are using EF4. 95% of our string columns in our db are varchar, but we do have 5% being nvarchar. In the edmx file, I see the columns have the proper Unicode property set to true or false. Then we use .tt file to generate our entity classes. The problem is that the generated queries are trying to convert everything to unicode which is obviously slowing down all of our queries.

I found the following answers here but I don't believe they will help me. The first is using ColumnAttribute but from what I can see, this was not available until v4.1. The second seems like it overrides on a global level (although I don't understand where). Because we do have some nvarchar columns, I don't think this will work either. I've also seen use of AsNonUnicode() method. I have not fully researched if this is available in v4 because that seems like it needs to be used specifically every time we send a query. This is a large application so this would be a huge undertaking. Are these my only options here? Am I missing something? Any advice is appreciated.

Entity Framework Data Annotations Set StringLength VarChar

EF Code First - Globally set varchar mapping over nvarchar

Community
  • 1
  • 1
heath
  • 1,047
  • 1
  • 14
  • 31
  • Try `AsNonUnicode` as explained here: http://stackoverflow.com/questions/5828621/ef-4-produces-unicode-string-constans-in-sql-where-the-column-type-is-varchar-h I think what you are observing is a bug in EF that was fixed post EF4 – Pawel Feb 11 '16 at 18:58
  • Sorry, that's what I meant in my post and I have edited it. By using this, I need to use this method everywhere that these properties are queried? That is my fear as there are likely thousands of these across the application. – heath Feb 11 '16 at 21:53
  • Confirm if this is really it and try moving to EF5 if it works then? Actually, are you really on .NET Framework 4? Because if this is the issue I am thinking it is it would get fixed automatically if you moved to .NET Framework 4.5 (.NET Framework 4.5 contains EF5) and I think .NET Framework 4 is no longer supported. – Pawel Feb 11 '16 at 21:58
  • Yes, we are on .NET4 and EF4. It is a very large application so assuming upgrade is not an option, is my only choice to use AsNonUnicode() on every call? – heath Feb 11 '16 at 23:42
  • I am afraid it might be the case. You could possibly try to rewrite your queries to automatically include AsNonUnicode where necessary but it requires some ExpressionTree/Linq Provider kung fu - you can find some samples here http://stackoverflow.com/questions/1839901/how-to-wrap-entity-framework-to-intercept-the-linq-expression-just-before-executi – Pawel Feb 12 '16 at 00:16

0 Answers0