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 works 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
Contentproperty that limits its size by default.Try and decorate the
Contentproperty of your model with theStringLengthAttribute: