简体   繁体   English

为什么Entity Framework不为nvarchar(MAX)字段返回修剪后的字符串?

[英]Why does Entity Framework not return a trimmed string for a nvarchar(MAX) field?

I'm currently working on a Silverlight application connected to a SQL Server 2008 Express database that is largely based on a demo on Brad Abrams' blog . 我目前正在研究连接到SQL Server 2008 Express数据库的Silverlight应用程序,该数据库很大程度上基于Brad Abrams博客的演示 (NOTE: The demo file at the specified link is actually a different solution that the one he shows on the live running application . The demo in the blog post uses Entity Framework, while the live application uses POCO). (注意:指定链接上的演示文件实际上是他在实时运行的应用程序上显示的解决方案的另一种解决方案。博客文章中的演示使用Entity Framework,而实时应用程序使用POCO)。

I am trying to figure out why Entity framework is returning a fixed width string for fields that are set up as nvarchar(MAX). 我试图弄清楚为什么实体框架为设置为nvarchar(MAX)的字段返回固定宽度的字符串。 (By "Fixed Width," I mean something like "DC ", where there's white space at the end of the content of the string). (通过“固定宽度”,我的意思是类似“ DC”的内容,在字符串内容的末尾有空格)。

In the demo file, which is connected to an included MDF file, the Name field is a nvarchar(max) field, and entity framework correctly returns the trimmed string. 在连接到包含的MDF文件的演示文件中,“名称”字段为nvarchar(max)字段,并且实体框架正确返回修剪后的字符串。 However, in my personal application, which is connected to a SQLEXPRESS database, a nvarchar(max) field is still returned with the white space at the end, fixed width, and untrimmed. 但是,在我的个人应用程序(连接到SQLEXPRESS数据库)中,仍然返回nvarchar(max)字段,该字段的末尾带有空格,固定宽度并且未修剪。 When I hover over the return this._itemPrefix; 当我将鼠标悬停在return this._itemPrefix; line of Entity Model C# file at runtime, it shows the item with the white space, untrimmed. 实体模型C#文件在运行时的第一行,它显示带有空白的项目,未修剪。 I can't figure out why it works in his demo but not in my file. 我无法弄清楚为什么它在他的演示中起作用,但在我的文件中却不起作用。

Is there something I can do other than alter the code in the entityModel.Designer.cs to have it return a trimmed string? 除了更改entityModel.Designer.cs中的代码以使其返回修剪后的字符串之外,我还能做些其他的事情吗? Has anyone else ran into this issue? 还有其他人遇到过这个问题吗?

How many spaces? 有多少空间? The only reason I can think of, is because you stored the string that way. 我能想到的唯一原因是因为您以这种方式存储了字符串。
Working as intended. 按预期工作。

You should trim your strings with Trim() or TrimEnd() before you store them if you're using Entity Framework. 如果使用实体框架,则在存储字符串之前,应使用Trim()TrimEnd()修剪字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#实体框架-长字符串未作为NVARCHAR(MAX)保留 - C# Entity Framework - Long String not being persisted as NVARCHAR(MAX) 使用Dapper获取nvarchar(max)会返回一个修剪为4000个字符的字符串。这种行为可以改变吗? - Using Dapper to get nvarchar(max) returns a string trimmed to 4000 characters. Can this behaviour be changed? 指定MaxLength时,实体框架使用nvarchar(max) - Entity Framework uses nvarchar (max) when MaxLength is specified 为什么实体框架返回空List &lt;&gt;而不是空? - Why does Entity Framework return null List<> instead of empty ones? 实体框架不返回null - Entity framework does not return null 使用Entity Framework 6.1 IndexAttribute属性声明字符串字段键时指定最大长度吗? - Specify max length when declaring string field key with the Entity Framework 6.1 IndexAttribute attribute? 为什么实体框架在保存到数据库时截断字符串值? - Why does Entity Framework truncate string value while saving into database? Entity Framework中按字符串的MIN和MAX值 - MIN and MAX value by string in Entity Framework 实体框架和连接字符串最大超时 - Entity Framework and connection string max Timeouts 为什么MAX无法在Entity Framework中的何处使用? - Why MAX is not working with where in Entity Framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM