加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

.NET MVC:如何在SQL CE的Code-First中定义ntext字段?

发布时间:2020-12-12 06:49:11 所属栏目:MsSql教程 来源:网络整理
导读:我有以下型号: public class Blog{ public int BlogID { get; set; } public int CategoryID { get; set; } [MaxLength(70)] [Required] public string BlogTitle { get; set; } [Column(TypeName="ntext")] public string BlogContent { get; set; }} 我已
我有以下型号:
public class Blog
{
    public int BlogID { get; set; }
    public int CategoryID { get; set; }

    [MaxLength(70)]
    [Required]
    public string BlogTitle { get; set; }

    [Column(TypeName="ntext")]
    public string BlogContent { get; set; }
}

我已在SQL CE4数据库中手动将字段BlogContent设置为ntext类型(16字节).

但是,每次我尝试插入超过4000个字符的文本时,都会出现以下错误:

Validation failed for one or more
entities. See ‘EntityValidationErrors’
property for more details

我已经尝试为[Column(TypeName =“ntext”)]设置注释,但这没有区别.当我通过EntityValidationErrors集合循环时,问题是由BlogContent引起的,错误说:

String cannot be longer than 4000 characters

如何定义我的模型以获得BlogContent的ntext字段?

似乎忽略了任何数据注释;假设没有MaxLength的字符串默认限制为4000个字符.

解决方法

我已经解决了,你需要使用:
[Column(TypeName="ntext")]
[MaxLength]
public string BlogContent { get; set; }

详情请见:
http://www.cloudonedesign.com/Blog/Post/how-to-define-ntext-fields-using-code-first-in-net-30

In order to create an ntext column in the database,and allow model validation
to actually know that the string length can be more than 4,000 characters,we
have to use these two items:

[Column(TypeName="ntext")]: This will tell Code-First to generate an
ntext field in the database.

[MaxLength]: By using the default
constructor,it will take the maximum length of the database field,
instead of guessing the maximum length for a string,which is 4,000. If this is
missing or you explicitly set the maximum length such as [MaxLength(8000)],model validation will raise errors saying “String maximum length is 4,000 characters”.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读