mvc insert在实体框架中不能为null错误

时间:2015-04-18 20:13:11

标签: c# asp.net-mvc linq entity-framework razor

我收到一个异常,说用户电子邮件不能为空。 User.Identity.Name;设置为用户电子邮件地址,但是当我尝试创建新帖子时,会抛出异常说:

Cannot insert the value NULL into column 'BlogUserEmail', table
'Blog.dbo.Posts'; column does not allow nulls. INSERT fails.\r\nThe
statement has been terminated.

我不确定如何使用User.Identity.Name;将用户电子邮件地址添加到Post创建方法。它在使用User.Identity.Name;的身份验证方法中正常工作。但是我在Entity Framework 5中创建了一些额外的表,在Post表中我将BlogUserEmail作为外键:

![在此处输入图片说明] [1]

查看:

@model MyBlogger.Post

@{
    ViewBag.Title = "Create";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Create</h2>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Post</legend>

        <div class="editor-label"> // I changed this area
            @Html.LabelFor(model => model.BlogUserEmail, User.Identity.Name)
        </div>
        <div class="editor-field">
            @Html.ValidationMessageFor(model => model.BlogUserEmail)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.CategoryId, "Category")
        </div>
        <div class="editor-field">
            @Html.DropDownList("CategoryId", String.Empty)
            @Html.ValidationMessageFor(model => model.CategoryId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Title)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Title)
            @Html.ValidationMessageFor(model => model.Title)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ShortDescription)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ShortDescription)
            @Html.ValidationMessageFor(model => model.ShortDescription)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Description)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Description)
            @Html.ValidationMessageFor(model => model.Description)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Meta)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Meta)
            @Html.ValidationMessageFor(model => model.Meta)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.UrlSlug)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.UrlSlug)
            @Html.ValidationMessageFor(model => model.UrlSlug)
        </div>


        <div class="editor-label">
            @Html.LabelFor(model => model.Published)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Published)
            @Html.ValidationMessageFor(model => model.Published)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.PostedOn)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.PostedOn)
            @Html.ValidationMessageFor(model => model.PostedOn)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Modified)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Modified)
            @Html.ValidationMessageFor(model => model.Modified)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>

2 个答案:

答案 0 :(得分:3)

由于你是从User.Identity.Name中选择它,你可以在HttpPost方法中分配它。

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Post post)
{
    post.BlogUserEmail = User.Identity.Name
    try
    {
        if (ModelState.IsValid)
        {
            db.Posts.Add(post);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        var email = User.Identity.Name;
        ViewBag.BlogUserEmail = new SelectList(db.BlogUsers, email);
        ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", post.CategoryId);
        return View(post);
    }
    catch (DbEntityValidationException e)
    {
        var newException = new FormattedDbEntityValidationException(e);
        throw newException;
    }

}

由于它不是“创建视图”中的可编辑字段,因此您还可以采用放入HiddenField的方法。

请记住,只读标签不会在Post方法中绑定到您的模型。

答案 1 :(得分:1)

如果可以在帖子表单中更改电子邮件,可能不是现在,但是及时,您可以将其添加为带有默认值的隐藏字段。

@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
    <legend>Post</legend>

    <div class="editor-label"> // I changed this area
        @Html.LabelFor(model => model.BlogUserEmail, User.Identity.Name)
    </div>
    <div class="editor-field">
        @Html.ValidationMessageFor(model => model.BlogUserEmail)
    </div>
    @* Hidden Field *@
    @Html.HiddenFor(model => model.BlogUserEmail,new { value = User.Identity.Name })

    // rest of the html