问题描述
我有什么应该是一个简单的问题,但我一直无法找到自己的答案。
I have what should be an easy question but I have been unable to find the answer myself.
我使用EF4 CTP-5的Code First模型手产生波苏斯。据处理字符串比较在生成的SQL为
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;}
哪些修复该问题为单一事件,并正确生成SQL为:
Which fixes the issue for that single occurrence and correctly generates the SQL as:
WHERE 'Value' = Object.Property
不过,我处理的一个非常大的领域模型,并通过每串去场和设置的TypeName =VARCHAR将是非常非常繁琐。我想指定EF应该看到的字符串作为全线VARCHAR,因为这是在这个数据库的标准和nvarchar是例外情况。
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.
推理想纠正这是查询执行效率。 VARCHAR和nvarchar的比较是在SQL Server 2K5,其中VARCHAR为varchar比较几乎立即执行效率非常低。
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.
推荐答案
EF 4.1之前,你可以使用约定,并添加以下约定到你的模型构建器:
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;
}
}
(从的)
更新:可插拔公约是取消了对4.1版本。 一个替代方法)
UPDATE: Pluggable conventions were dropped for the 4.1 release. Check my blog for an alternative approach)
这篇关于EF代码优先 - 在全球范围内为nvarchar设置VARCHAR映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!