实体框架 - 获取文本列中的数据长度 [英] Entity Framework - Getting the length of data in a text column

查看:124
本文介绍了实体框架 - 获取文本列中的数据长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用一个使用文本字段的表格时,我遇到了一个错误。

I've hit an error when I've been working on a table that uses a text field.

如果我得到一个varchar列的长度,我可以做

If I was getting the length of a varchar column I could do

var result =(from t in context.tablename select t.fullname.Length)

但是,如果我在文本字段上运行相同的查询:

However, if I run the same query on a text field:

var result =(from t in context.tablename select t.biography.Length)

我收到错误:

参数数据类型文本对len函数的参数1无效

做了一点阅读该主题我了解为什么SQL Server引发了这个错误,但我不确定周围最好的方法。我知道我可以返回结果,然后得到结果字符串的长度,但肯定有一个更简单的方法来做到这一点?

Having done a bit of reading up on the subject I understand why SQL Server raises this error but I'm not sure of the best way around it. I know I could return the result and then get the length of the resulting string but surely there is an easier way of doing this?

推荐答案

p>我认为你最好的选择是将列数据类型更新为 VARCHAR(MAX),如果它是 TEXT NVARCHAR(MAX)如果它是 NTEXT 。有很多资源如何做到这一点,但通常你会创建一个新的 [N] VARCHAR(MAX)列,然后将所有的数据更新到新的列,然后删除旧列,最后将新列重命名为旧名称。

I think your best option is to update the column data type to VARCHAR(MAX) if it is TEXT or NVARCHAR(MAX) if it is NTEXT. There are plenty of resources on how to do this, but generally you make a new column of [N]VARCHAR(MAX) and then you update all your data across into the new column, then drop the old column and finally rename the new column to the old name.

如果不能更改表模式,则需要创建一个视图,然后在该视图的选择中进行类型转换。但是,您也可以根据上述更改列数据类型(除非您不是数据库所有者,并在不同的数据库中创建视图)。但是请注意,EF并不总是像观看表一样玩得很好。

If you can't change the table schema, then you will need to create a view and do the type casting in the select of that view.. but then you might as well have just changed the column data type as mentioned above (unless you're not the db owner and you create the view in a different database). But be mindful that EF doesn't always play as nice with views as it does with tables.

这篇关于实体框架 - 获取文本列中的数据长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆