实体框架6.0 NVARCHAR与VARCHAR

发布于 2025-01-30 03:12:45 字数 1649 浏览 2 评论 0原文

我有一个在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开雨落又逢春i 2025-02-06 03:12:45

如果使用注释,请考虑分别指定列和最大长度。

[Required]
[Column(TypeName = "varchar")]
[MaxLength(20)]
string MyString { get; set; }

If using annotations, consider specifying the Column and the MaxLength separately.

[Required]
[Column(TypeName = "varchar")]
[MaxLength(20)]
string MyString { get; set; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文