0
I’m with a exception
, in the method of creating an object with a foreign key, db.SaveChanges()
with the following message:
Dbupdateexception was unhandled by user code.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "CursoId,NomeCurso,Creditos,UniversidadeId")] Curso curso)
{
if (ModelState.IsValid)
{
db.Cursos.Add(curso);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.UniversidadeId = new SelectList(db.Universidades, "UniversidadeId", "NomeUniversidade", curso.UniversidadeId);
return View(curso);
}
My view Create:
@model PortalEscola.WEB.Models.Curso
@{
ViewBag.Title = "Create";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Criar curso</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.NomeCurso, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.NomeCurso, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.NomeCurso, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Creditos, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Creditos, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Creditos, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UniversidadeId, "UniversidadeId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("UniversidadeId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.UniversidadeId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Salvar" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Voltar", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
And my course class is like this:
public class Curso
{
public int CursoId { get; set; }
[Display(Name = "Nome do Curso")]
public string NomeCurso { get; set; }
public int Creditos { get; set; }
public int UniversidadeId { get; set; }
[ForeignKey("UniversidadeId")]
[Display(Name = "Universidade")]
public virtual Universidade Universidade { get; set; }
public virtual ICollection<Inscricao> Inscricoes { get; set; }
}
I can save the university alterations, DropDownList
with a University.but when I register the Course, db.SaveChanges
,
in the method ActionResult
Create
, to register the course.
What’s the mistake? ...
– novic
Open the exception window pointing to the db.Savechanges() method; In Actionresult Create() , when I register the data of the Course class with the following message: Dbupdateexception was unhandled by user codeAn Exception of type 'System.Data.Entity.Infrastructure.Dbupdateexception' occurred in Entityframework.dll but was not handled in user code Additional information: An error occurred while updating the Entries. See the Inner Exception for Details
– J. Moura
At the point of exception open the attributes and look for Innerexception
– Leandro Angelo
I put in the Course class a Dataannotation in the Cursoid field [Databasegenerated(Databasegeneratedoption.None)] I did a Re-build on the application, {"Cannot insert an explicit value for the identity column in table 'Table' when IDENTITY_INSERT is set to OFF."}
– J. Moura