实体框架6.0 NVARCHAR与VARCHAR
我有一个在Azure运行的网站,这引起了我的头痛。我认为选择查询是来自EF的Azure中昂贵的查询。
该查询是针对数据库中的varchar
列的选择,但是从EF中,它将其转换为nvarchar(4000)
,使查询超时。
这是我在代码中调用查询的方式:
var myObject = db.MyTable.FirstOrDefault(cm => cm.MyField == entryModel.MyField.Trim());
我已经搜索了许多资源,解释了EF如何使用nvarchar
而不是varchar
,所以我尝试添加此注释在我的模型类中的字段中:
[Column(TypeName = "varchar(50)")]
当那不起作用时,我尝试将其添加到onModeLcreating
方法:
modelBuilder.Entity<MyTable>().Property(x => x.MyField).IsUnicode(false);
我也尝试将其强制所有字符串将其强加于varchar
:
modelBuilder.Properies<string>().Configure(x => x.HasColumnType("varchar(1000)"));
该查询仍在引起性能问题,根据数据库中的活动监视器,查询仍在显示:
(@p__linq__0 nvarchar(4000))SELECT TOP (1)
blah blah fields
FROM [dbo].[MyTable] AS [Extent1]
WHERE ([Extent1].[MyField] = (LTRIM(RTRIM(@p__linq__0)))) OR (([Extent1].[MyField] IS NULL) AND (LTRIM(RTRIM(@p__linq__0)) IS NULL))
有关导致此原因的什么想法?我现在正要写一个存储过程以致电,而不是使用此EF查询。
编辑 感谢 @robert harvey and @StriplingWarrior 提供帮助。卸下内联装饰允许EF识别VARCHAR更改,并从实际的SQL调用中删除装饰。
var myField = entryModel.MyField.Trim();
var myObject = db.MyTable.FirstOrDefault(cm => cm.MyField == myField);
谢谢!
亚伦
I have a site running in Azure that is causing me headaches. I see a select query as being an expensive query in Azure that is coming from EF.
The query is a select against a varchar
column in the database, but from EF, it is turning it into nvarchar(4000)
, causing the query to time out.
Here is how I'm calling the query in the code:
var myObject = db.MyTable.FirstOrDefault(cm => cm.MyField == entryModel.MyField.Trim());
I have searched through many resources explaining how EF uses nvarchar
instead of varchar
, so I've tried adding this annotation to the field in my model class:
[Column(TypeName = "varchar(50)")]
When that didn't work, I tried adding this to the OnModelCreating
method:
modelBuilder.Entity<MyTable>().Property(x => x.MyField).IsUnicode(false);
I've also tried this to force all strings to varchar
:
modelBuilder.Properies<string>().Configure(x => x.HasColumnType("varchar(1000)"));
The query is still causing performance issues that, according to the activity monitor in the database, the query is still showing as this:
(@p__linq__0 nvarchar(4000))SELECT TOP (1)
blah blah fields
FROM [dbo].[MyTable] AS [Extent1]
WHERE ([Extent1].[MyField] = (LTRIM(RTRIM(@p__linq__0)))) OR (([Extent1].[MyField] IS NULL) AND (LTRIM(RTRIM(@p__linq__0)) IS NULL))
Any ideas as to what is causing this? I'm at a point right now that I'm about to just write a stored procedure to call instead of using this EF query.
EDIT
Thanks to @Robert Harvey and @StriplingWarrior for their help. Removing the inline trim allowed EF to recognize the varchar change as well as removing the TRIM from the actual SQL calls.
var myField = entryModel.MyField.Trim();
var myObject = db.MyTable.FirstOrDefault(cm => cm.MyField == myField);
Thanks!
Aaron
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果使用注释,请考虑分别指定列和最大长度。
If using annotations, consider specifying the Column and the MaxLength separately.