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

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

问题描述

我面对一个令人困惑的问题,即在我的编辑或创建行动结果的方法,EF4将抛出DbEntityValidationException与内消息说明:


  

本场车身必须是字符串或
  阵列型用的最大长度
  '128'。


在讨论的模式是这样的:

  [表(tblArticles)]
公共类文章
{
    [键]
    公众诠释ID {搞定;组; }
    [必需(的ErrorMessage =标题必须包含)]
    公共字符串名称{搞定;组; }
    [AllowHtml]
    公共字符串车身{搞定;组; }
    [必需(的ErrorMessage =起始日期必须指定)]
    [显示(NAME =起始日期)]
    [DisplayFormat(DataFormatString =DD-MM-YYYY)]
    公众的DateTime?起始日期{搞定;组; }
    [必需(的ErrorMessage =结束日期必须指定)]
    [显示(NAME =结束日期)]
    公众的DateTime?结束日期{搞定;组; }
    公众诠释优先{搞定;组; }
    公共BOOL归档{搞定;组; }    公共虚拟的ICollection< ArticleImage>图片{搞定;组; }
}

在实际的数据库中的身体字段类型文本的,所以没有明显的限制存在。我想要发布的数据是这样的:


 < P>
这是一个例子,以确认新文章在寻找右LT; / P>
&所述p为H.;
&所述; IMG ALT =SRC =htt​​p://www.google.co.nz/logos/2011/houdini11-sr.jpg
风格=宽度:160像素,高度:56px;浮动:左; />&下; / P>


编辑方法的一个例子是这样的:

  [HttpPost]
公众的ActionResult编辑(第文章)
{
    如果(ModelState.IsValid)
    {
        尝试
        {
            articleRepository.Update(条);
        }
        赶上(DbEntityValidationException dbevEx)
        {
            。ErrorSignal.FromCurrentContext()提高(dbevEx);
            ModelState.AddModelError(形式,dbevEx);
            返回视图(编辑,文章);
        }
        //其他异常处理情况...
    }    返回RedirectToAction(「指数」);
}

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

 公共无效更新(T实体)
{
    dbset.Attach(实体);
    db.Entry(实体).STATE = System.Data.EntityState.Modified;
    db.Commit();
}

我看不到code或数据库中的任何可能会引起问题,所以还有什么地方我应该看看?


解决方案

在code字符串字段的默认长度首先是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天全站免登陆