【问题标题】:Entity Framework Validation confusion - maximum string length of '128'实体框架验证混淆 - '128' 的最大字符串长度
【发布时间】:2011-07-21 20:08:44
【问题描述】:

我遇到了一个令人困惑的问题,在我的 Edit 或 Create action result 方法中,EF4 将抛出一个 DbEntityValidationException ,内部消息说明:

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

有问题的模型如下所示:

[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; }
}

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

<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>

Edit 方法的示例如下所示:

[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");
}

最后,实际完成 grunt 工作的方法是:

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

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

【问题讨论】:

    标签: entity-framework asp.net-mvc-3 entity-framework-4.1


    【解决方案1】:

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

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

    这篇文章变得很受欢迎,所以我添加了第二种方法,它也有效:

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

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

    【讨论】:

    • 魔术。这就是问题所在。谢谢。
    • 这解决了我的问题,但在我们的例子中,它是某些部署的问题,而不是其他部署。结果发现部署了稍微不同的 EF 4.1 dll 版本。
    • 或者用fluentconfig:Property(a => a.Body).IsMaxLength();
    【解决方案2】:

    如果您使用“模型优先”方法遇到此错误,请检查 EF 模型:可能只是您正在更新的实体上的属性设置了 Max Length 属性。

    【讨论】:

      【解决方案3】:

      可能你用过

      Property(m => m.Body ).IsRequired().HasMaxLength(128);
      

      在 OnModelCreating 中的 dbcontext 类上。 所以根据你的长度改变

      【讨论】:

        【解决方案4】:

        对我来说,[MaxLength] 不起作用。我在上下文中添加了以下语句。

        modelBuilder.Entity<YourModel>().Property(e => e.YourColumn).HasMaxLength(4000);
        

        我没有尝试超过“4000”限制,但我认为它可以进一步扩展。您不必保留它 128 作为编译器显示的错误。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-08-15
          • 1970-01-01
          • 1970-01-01
          • 2012-06-14
          • 1970-01-01
          • 1970-01-01
          • 2014-11-27
          相关资源
          最近更新 更多