C#学习教程:EFCodeFirst-在nvarchar上全局设置varchar映射我将EF4CTP-5CodeFirst模型与手动生成的POCO一起使用。它在生成的SQLWHEREN'Value'=Object.Property中处理字符串比较我知道我可以使用以下方法覆盖此功能:[Column(TypeName="varchar")]publicstringProperty{get;set;}这解决了问题该单个事件并正确生成SQL为:WHERE'Value'=Object.Property但是,我正在处理一个非常大的域模型并遍历每个字符串字段并设置TypeName="varchar"将非常非常麻烦。我想指定EF应该全面地将字符串视为varchar,因为这是该数据库中的标准,nvarchar是个例外。想要纠正这个问题的原因是查询执行效率。在SQLServer2k5中,varchar和nvarchar之间的比较效率非常低,其中varchar与varchar的比较几乎是立即执行的。在EF4.1之前,您可以使用约定并将以下约定添加到ModelBuilder:使用System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive;使用System.Data.Entity.ModelConfiguration.Conventions.Configuration;使用System.Reflection;公共类MakeAllStringsNonUnicode:IConfigurationConvention{publicvoidApply(PropertyInfopropertyInfo,Funcconfiguration){configuration().IsUnicode=false;}}(来自http://blogs.msdn.com/b/adonet/archive/2011/01/10/ef-feature-ctp5-pluggable-conventions.aspx)更新:可插入约定在版本4.1中被删除。检查我的博客是否有替代方案)我扩展了MarcCals的回答(和Diego的博客文章)以根据问题将所有实体上的所有字符串全局设置为非unicode,而不是按类别手动调用它。见下文。//////将给定实体的所有字符串属性的“默认”更改为varchar而不是nvarchar。/////////protectedvoidSetAllStringPropertiesAsNonUnicode(DbModelBuildermodelBuilder,TypeentityType){varstringProperties=entityType.GetProperties().Where(c=>c.PropertyType==typeof(string)&&c.PropertyType.IsPublic&&复制代码c.CanWrite&&!Attribute.IsDefined(c,typeof(NotMappedAttribute)));foreach(PropertyInfopropertyInfoinstringProperties){dynamicpropertyExpression=GetPropertyExpression(propertyInfo);MethodInfoentityMethod=typeof(DbModelBuilder).GetMethod("Entity");MethodInfogenericEntityMethod=entityMethod.MakeGenericMethod(entityType);objectentityTypeConfiguration=genericEntityMethod.Invoke(modelBuilder,null);MethodInfopropertyMethod=entityTypeConfiguration.GetType().GetMethod("属性",newType[]{propertyExpression.GetType()});StringPropertyConfiguration属性=(StringPpropertyConfiguration)propertyMethod.Invoke(entityTypeConfiguration,newobject[]{propertyExpression});property.IsUnicode(false);}}privatestaticLambdaExpressionGetPropertyExpression(PropertyInfopropertyInfo){varparameter=Expression.Parameter(propertyInfo.reflectedType)Lambda(Expression.Property(parameter,propertyInfo),parameter);}//////返回“this”上下文中所有DbSet实体类型的可枚举。/////////privateIEnumerableGetEntityTypes(){returnthis.GetType().GetProperties().Where(a=>a.CanWrite&&a.PropertyType.IsGenericType&&a.PropertyType.GetGenericTypeDefinition()==typeof(DbSet)).Select(a=>a.PropertyType.GetGenericArguments().Single());最后,从您的OnModelCreating(DbModelBuilder模型构建器)中调用它:这是SergeyBarskiy的项目,EF已扩展为允许自定义约定,因此,您可以创建自定义属性而不是流畅的API这是此处的代码片段,演示了该实用程序的实际应用。您在这里看不到的是小数精度属性和其他属性。所以根据你的问题,一旦你将Unicode设置为false,它应该是varchar而不是nvarchar。公共类产品{publicintProductId{get;放;}[Indexed("Main",0)]publicstringProductNumber{get;放;}[Indexed("Main",1)][Indexed("Second",direction:IndexDirection.Ascending)][Indexed("Third",direction:IndexDirection.Ascending)]publicstringProductName{get;放;}[String(4,12,false)]//minLength,maxLength,isUnicodepublicstring说明{get;放;}[Indexed("Third",1,direction:IndexDirection.Descending)]publicboolIsActive{get;放;}[Default("0")]公共小数?价格{得到;放;}[默认(“GetDate()”)]公共日期时间?添加日期{得到;放;}[默认(“20”)]publicintCount{get;放;阅读本文和详细信息。借助Diego的博客,不使用anotations制作POCOvarchar的publicproperties是:需要详细了解C#学习教程,希望大家多多关注—privatevoidSetStringPropertiesAsNonUnicode(DbModelBuilder_modelBuilder)wheree:class{//IndiquematoteslespropietatsstringquenosónunicodeperaqueescreincomavarcharListstringProperties=typeof(e).GetProperties().Where(c=>c.PropertyType==typeof(string)&&c.PropertyType.IsPublic).ToList();foreach(PropertyInfopropertyInfoinstringProperties){dynamicpropertyExpression=GetPropertyExpression(propertyInfo);_modelBuilder.Entity().Property(propertyExpression).IsUnicode(false);}}//编辑:还从引用的博客文章(Scott)中窃取了这个返回Expression.Lambda(Expression.Property(参数,propertyInfo),参数呃);}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处:
