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

前端之家收集整理的这篇文章主要介绍了.NET MVC:如何在SQL CE的Code-First中定义ntext字段?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下型号:
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@H_301_10@ entities. See ‘EntityValidationErrors’@H_301_10@ 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; }

详情请见:@H_301_10@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@H_301_10@ to actually know that the string length can be more than 4,000 characters,we@H_301_10@ have to use these two items:

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

[MaxLength]: By using the default@H_301_10@ constructor,it will take the maximum length of the database field,@H_301_10@ instead of guessing the maximum length for a string,which is 4,000. If this is@H_301_10@ 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”.

原文链接:https://www.f2er.com/mssql/77750.html

猜你在找的MsSQL相关文章