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.
With Diego's blog help, to make the public properties of a POCO varchar without using anotations is :
Here is a project from Sergey Barskiy that extends EF to allow custom conventions, which as a result, you can make custom attributes instead of the fluent API.
Here is a code snippet from here that demonstrates the utility in action. What you don't see here is the decimal precision attribute and others. So per your question, once you set Unicode to false, it should be
varchar
as opposed tonvarchar
.Read this and this for detail.
Before EF 4.1, you could use conventions and add the following convention to your ModelBuilder:
(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)
I extended Marc Cals' answer (and Diego's blog post) to globally set all strings on all entities as non-unicode as per the question, rather than having to call it manually per-class. See below.
Finally, call it from your OnModelCreating(DbModelBuilder modelBuilder):