Victor Semeniuk Victor Semeniuk - 4 months ago 13
ASP.NET (C#) Question

ASP.NET MVC 5, INSERT not work in Entity Framework

My job is to make a website for a school management system. I have students and admins who can log in the system. Students can register. I have created controller for ASP.NET MVC 5 using template for Entity Framework. It have created Account model, Controller and Create, Delete, Details, Edit, Index views. Fields of account are:

public int AccountID { get; set; }
public Nullable<int> TypeID { get; set; }
public string Password { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string ICNumber { get; set; }
public string Telephone { get; set; }
public string email { get; set; }
public string Adress { get; set; }`


I have changed my views to hide information that does not need to shown, as ID, password etc. (My code =>)

@{
ViewBag.Title = "Register";
}
@model WebApplication9.Account


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

<div class="form-horizontal">
<h4>Account</h4>
<hr />

<div class="form-group">
@Html.LabelFor(model => model.TypeID, "TypeID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("TypeID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.TypeID, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.ICNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ICNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ICNumber, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Telephone, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Telephone, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Telephone, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.email, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Adress, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Adress, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Adress, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}


Account ID should be autogenereted in database, I have also used Identity as StoreGenerated Pattern in Account Model but Im getting error while trying to registrate new student:


SQLException: Cannot insert the value NULL into column 'AccountID'


Controller code:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register([Bind(Include = "AccountID,TypeID,Password,FirstName,LastName,ICNumber,Telephone,email,Adress")] Account account)
{
if (ModelState.IsValid)
{
db.Accounts.Add(account);
db.SaveChanges();
return RedirectToAction("Index");
}

ViewBag.TypeID = new SelectList(db.AccountTypes, "TypeID", "TypeName", account.TypeID);
return View(account);
}

Answer Source

Accout Model

Try adding the [Key] attribute and make sure your database is setup as an auto incremented field.

[Key]
public int AccountID { get; set; }

Controller

You could also remove the AccountID from your [Bind(Include = "")] as this is not being set by the post and being managed on the server. This should not be causing the error, but would be good practice to do.

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register([Bind(Include = "TypeID,Password,FirstName,LastName,ICNumber,Telephone,email,Adress")] Account account)
{
    // stuff
}

SQL Server

On your SQL Server the AccountID should be set to Yes for Identity Specification and (Is Identity) should be Yes. Identity Increment is usually 1 and identity seed is usually 1 but those should not be causing any issues with your insert.