NOT NULL column in migration files

Topics: Core, Customizing Orchard
May 30, 2014 at 9:01 AM
Hi!
I created column with not null restriction in migration class
.Column<string>("Name", c => c.NotNull())
Also I've add an "Required" attribute for corresponding property in my content part class.
Then I took this code from blog module (BlogPostAdminController.cs)
private ActionResult CreatePOST(int blogId, bool publish = false) {
    var blog = _blogService.Get(blogId, VersionOptions.Latest).As<BlogPart>();

    if (blog == null)
        return HttpNotFound();

    var blogPost = Services.ContentManager.New<BlogPostPart>("BlogPost");
    blogPost.BlogPart = blog;

    if (!Services.Authorizer.Authorize(Permissions.EditBlogPost, blog, T("Couldn't create blog post")))
        return new HttpUnauthorizedResult();
    
    Services.ContentManager.Create(blogPost, VersionOptions.Draft);
    var model = Services.ContentManager.UpdateEditor(blogPost, this);

    if (!ModelState.IsValid) {
        Services.TransactionManager.Cancel();
        return View(model);
    }

    if (publish) {
        if (!Services.Authorizer.Authorize(Permissions.PublishBlogPost, blog.ContentItem, T("Couldn't publish blog post")))
            return new HttpUnauthorizedResult();

        Services.ContentManager.Publish(blogPost.ContentItem);
    }

    Services.Notifier.Information(T("Your {0} has been created.", blogPost.TypeDefinition.DisplayName));
    return Redirect(Url.BlogPostEdit(blogPost));
}
and make some changes:
public ActionResult CreateCompanyPost()
{
    if (!_orchardServices.Authorizer.Authorize(Permissions.CreateCompany, T("Not allowed to create company")))
        return new HttpUnauthorizedResult();

    var companyPart = _orchardServices.ContentManager.New<CompanyPart>("Company");
    if (companyPart == null)
        return HttpNotFound();

    _orchardServices.ContentManager.Create(companyPart, VersionOptions.Draft);
    var model = _orchardServices.ContentManager.UpdateEditor(companyPart, this);
    
    if (!ModelState.IsValid)
    {
        _orchardServices.TransactionManager.Cancel();
        return View("CompanyItem", model);
    }

    _orchardServices.ContentManager.Publish(companyPart.ContentItem);
    _orchardServices.Notifier.Information(T("Company created"));
    return Redirect(Url.Action("ListCompanies", "CommonAdmin", new { area = "PersonalAccount" }));
    
}
Problem is when I'm trying to save null in Name field, I getting an error about insert null fails BEFORE model validation.
What should I do if I want to have both validation checks?
May 30, 2014 at 9:32 AM
Edited May 30, 2014 at 11:34 AM
Actualy, all I have to do is to move model validation code before creating content part, like this
public ActionResult CreateCompanyPost()
{
    if (!_orchardServices.Authorizer.Authorize(Permissions.CreateCompany, T("Not allowed to create company")))
        return new HttpUnauthorizedResult();

    var companyPart = _orchardServices.ContentManager.New<CompanyPart>("Company");
    if (companyPart == null)
        return HttpNotFound();

    var model = _orchardServices.ContentManager.UpdateEditor(companyPart, this);
    
    if (!ModelState.IsValid)
    {
        _orchardServices.TransactionManager.Cancel();
        return View("CompanyItem", model);
    }

    _orchardServices.ContentManager.Create(companyPart, VersionOptions.Published);
    _orchardServices.Notifier.Information(T("Company created"));
    return Redirect(Url.Action("ListCompanies", "CommonAdmin", new { area = "PersonalAccount" }));
    
}