实体框架验证混淆 - '128' 的最大字符串长度 [英] Entity Framework Validation confusion - maximum string length of '128'

查看:28
本文介绍了实体框架验证混淆 - '128' 的最大字符串长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个令人困惑的问题,在我的 Edit 或 Create 操作结果方法中,EF4 将抛出 DbEntityValidationException 并带有内部消息说明:

<块引用>

字段 Body 必须是字符串或最大长度为的数组类型'128'.

有问题的模型如下所示:

[Table("tblArticles")]公开课文章{[钥匙]公共 int ID { 获取;放;}[必填(ErrorMessage="必须包含标题")]公共字符串标题{获取;放;}[允许HTML]公共字符串正文{获取;放;}[必需(ErrorMessage="必须指定开始日期")][显示(名称=开始日期")][DisplayFormat(DataFormatString="dd-mm-yyyy")]公共日期时间?开始日期 { 获取;放;}[必需(ErrorMessage =必须指定结束日期")][显示(名称=结束日期")]公共日期时间?结束日期 { 获取;放;}公共 int 优先级 { 获取;放;}公共布尔存档{获取;放;}公共虚拟 ICollection图像 { 得到;放;}}

实际数据库中的正文"字段是文本类型,因此没有明显限制.我要发布的数据是这样的:

<块引用>

这是确认新文章看起来正确的示例.</p><p><img alt="" src="http://www.google.co.nz/logos/2011/houdini11-sr.jpg"样式=宽度:160px;高度:56px;浮动:左;"/></p>

Edit 方法的示例如下所示:

[HttpPost]公共 ActionResult 编辑(文章文章){如果(模型状态.IsValid){尝试{articleRepository.Update(文章);}捕获(DbEntityValidationException dbevEx){ErrorSignal.FromCurrentContext().Raise(dbevEx);ModelState.AddModelError("FORM", dbevEx);返回视图(编辑",文章);}//其他异常处理发生...}return RedirectToAction("索引");}

最后,实际执行繁重工作的方法是:

public void Update(T Entity){dbset.Attach(实体);db.Entry(Entity).State = System.Data.EntityState.Modified;数据库提交();}

我在代码或数据库中看不到任何可能导致问题的内容,那么我还应该在哪里查看?

解决方案

code first 中字符串字段的默认长度为 128.如果您使用 EF 验证,它将抛出异常.您可以使用以下方法扩展大小:

[StringLength(Int32.MaxValue)]公共字符串正文{获取;放;}

这篇文章在某种程度上很受欢迎,所以我添加了第二种方法,它也有效:

[最大长度]公共字符串正文{获取;放;}

StringLengthAttribute 来自 System.ComponentModel.DataAnnotations 程序集,MaxLengthAttribute 来自 EntityFramework 程序集 (EF 4.1).

I'm faced with a confusing problem where in my Edit or Create action result methods, EF4 will throw a DbEntityValidationException with the inner message stating:

The field Body must be a string or array type with a maximum length of '128'.

The model in question looks like this:

[Table("tblArticles")]
public class Article
{
    [Key]
    public int ID { get; set; }
    [Required(ErrorMessage="Title must be included")]
    public string Title { get; set; }
    [AllowHtml]
    public string Body { get; set; }
    [Required(ErrorMessage="Start Date must be specified")]
    [Display(Name="Start Date")]
    [DisplayFormat(DataFormatString="dd-mm-yyyy")]
    public DateTime? StartDate { get; set; }
    [Required(ErrorMessage = "End Date must be specified")]
    [Display(Name = "End Date")]
    public DateTime? EndDate { get; set; }
    public int Priority { get; set; }
    public bool Archived { get; set; }

    public virtual ICollection<ArticleImage> Images { get; set; }
}

The "Body" field in the actual database is of type Text, so there's no obvious limit there. The data that I'm trying to post is this:

<p>
This is an example to confirm that new articles are looking right.</p>
<p>
<img alt="" src="http://www.google.co.nz/logos/2011/houdini11-sr.jpg"
style="width: 160px; height: 56px; float: left;" /></p>

An example of the Edit method looks like this:

[HttpPost]
public ActionResult Edit(Article article)
{
    if (ModelState.IsValid)
    {
        try
        {
            articleRepository.Update(article);
        }
        catch (DbEntityValidationException dbevEx)
        {
            ErrorSignal.FromCurrentContext().Raise(dbevEx);
            ModelState.AddModelError("FORM", dbevEx);
            return View("Edit", article);
        }
        // Other exception handling happens...
    }

    return RedirectToAction("Index");
}

And finally, the method that actually does the grunt work is:

public void Update(T Entity)
{
    dbset.Attach(Entity);
    db.Entry(Entity).State = System.Data.EntityState.Modified;
    db.Commit();
}

I can't see anything in code or in the database that might be causing the problem, so where else should I look?

解决方案

Default length of string field in code first is 128. If you are using EF validation it will throw exception. You can extend the size by using:

[StringLength(Int32.MaxValue)]
public string Body { get; set; }

This post became somehow popular so I'm adding second approach which also works:

[MaxLength]
public string Body { get; set; }

StringLengthAttribute is from System.ComponentModel.DataAnnotations assembly and MaxLengthAttribute is from EntityFramework assembly (EF 4.1).

这篇关于实体框架验证混淆 - '128' 的最大字符串长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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