I am getting an exception that says the user email cannot be null. The User.Identity.Name; is set to the users email address however when I try to create a new post the exception is thrown saying:
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.
Im not sure how to add the users email address using the User.Identity.Name; to the Post create method. It works fine in my authentication method using User.Identity.Name;. However I created some extra tables in Entity Framework 5 and in the Post table I have the BlogUserEmail as a Foreign Key:
![enter image description here][1]
View:
@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>