开发者

MVC3 - Can't enter long text into database

开发者 https://www.devze.com 2023-02-22 13:08 出处:网络
I\'m making an extremely simple CMS in ASP.NET MVC 3 (just to learn it) but I\'ve run into complications.

I'm making an extremely simple CMS in ASP.NET MVC 3 (just to learn it) but I've run into complications. I'm using the EntityFramework (code first), and I'm trying to store articles in MySQL database (articles have standard properties like id, date, headline and content). My Create method is from some tutorial and looks as follows:

    [HttpPost]
    public ActionResult Create(Article newArticle)
    {
        if (ModelState.IsValid)
        {
            db.Articles.Add(newArticle);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        else
        {
            return View(newArticle);
        }
    }

I have generated a Create view (using razor engine) without changing anything. Everything wo开发者_运维技巧rks fine unless I try to enter longer text into the field for the "content" property (more than 20 or 30 words). Then I get a

System.Data.Entity.Validation.DbEntityValidationException was unhandled by user code

error at the db.SaveChanges() line. I have tried changing the column I use for storing articles' content to text and then to nvarchar(max) (using Visual Studio's Server Explorer, I hade to change some setting before it let me do that) but it made no difference.

Is there anything obvious I'm missing? I have pretty much no background in web and database development. Thanks in advance for any hints.


There is probably a mapping for the Content property that limits its size by default. Try and decorate the Content property of your model with the StringLengthAttribute:

public class Article
{
    [StringLength(4000)]
    public string Content { get; set; }
}


Is this using EF4.1 RC?

I believe that this might be caused by the default length of varchars, which is set to 128.

ado.net blog post - as stated in the post, you might need to set the maxlength higher .

I believe that this is tested by EF itself, and not at the DB level and that's why your change did not help.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号