Entity Framework Validation confusion - maximum string length of '128'

31,832

Solution 1

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

Solution 2

If you get this error using a Model First approach, check the EF Model: it may be simply that a property on the Entity you're updating has the Max Length attribute set.

Solution 3

May be you have used

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

On your dbcontext class in OnModelCreating. So change as per your length

Share:
31,832
Phil.Wheeler
Author by

Phil.Wheeler

I am a development manager for a Dunedin, New Zealand based technology company working in the education and life science sector. I've been a developer for over 10 years specialising in web applications on the Microsoft ecosystem and have since moved through project leadership, team leadership and management. In my spare time I blog a bit and do a little moonlighting as a professional consultant / developer. In whatever time I have left, I play the drums and get a bit of exercise in playing squash and cricket. Follow @Phil_Wheeler.

Updated on April 14, 2021

Comments

  • Phil.Wheeler
    Phil.Wheeler about 3 years

    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?