I have what should be an easy question but I have been unable to find the answer myself.
I am using EF4 CTP-5 Code First Model with hand generated POCOs. It is processing string comparisons in generated SQL as
WHERE N'Value' = Object.Property
I am aware that I can override this functionality using:
[Column(TypeName = "varchar")]
public string Property {get;set;}
Which fixes the issue for that single occurrence and correctly generates the SQL as:
WHERE 'Value' = Object.Property
However, I am dealing with a VERY large domain model and going through each string field and setting TypeName = "varchar" is going to be very very tedious. I would like to specify that EF should see string as varchar across the board as that is the standard in this database and nvarchar is the exception case.
Reasoning for wanting to correct this is query execution efficiency. Comparison between varchar and nvarchar is very inefficient in SQL Server 2k5, where varchar to varchar comparisons execute almost immediately.
Before EF 4.1, you could use conventions and add the following convention to your ModelBuilder:
using System;
using System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive;
using System.Data.Entity.ModelConfiguration.Conventions.Configuration;
using System.Reflection;
public class MakeAllStringsNonUnicode :
IConfigurationConvention<PropertyInfo, StringPropertyConfiguration>
{
public void Apply(PropertyInfo propertyInfo,
Func<StringPropertyConfiguration> configuration)
{
configuration().IsUnicode = false;
}
}
(Taken from http://blogs.msdn.com/b/adonet/archive/2011/01/10/ef-feature-ctp5-pluggable-conventions.aspx)
UPDATE: Pluggable conventions were dropped for the 4.1 release. Check my blog for an alternative approach)