I'm starting to use EntityFramework and MVC in C # .NET, and I have a problem trying to delete a record from a table that uses a composite key.
Driver:
// GET: DocenteCursoes/Delete/5
public ActionResult Delete(string curso, string docente)
{
if (curso == null && docente == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
DocenteCurso docenteCurso = db.DocenteCurso.Find(curso, docente);
if (docenteCurso == null)
{
return HttpNotFound();
}
return View(docenteCurso);
}
// POST: DocenteCursoes/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(string curso, string docente)
{
DocenteCurso docenteCurso = db.DocenteCurso.Find(curso, docente);
db.DocenteCurso.Remove(docenteCurso);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
Model:
[Table("DocenteCurso")]
public partial class DocenteCurso
{
[Key]
[Column(Order = 0)]
[StringLength(50)]
public string Curso { get; set; }
[Key]
[Column(Order = 1)]
[StringLength(50)]
public string Docente { get; set; }
}
Vista:
<div>
<h4>DocenteCurso</h4>
<hr />
<dl class="dl-horizontal">
</dl>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<div class="form-actions no-color">
<input type="submit" value="Delete" class="btn btn-default" /> |
@Html.ActionLink("Back to List", "Index")
</div>
}
I do not have problems with a primary key but with a composite key I do not know how to do it, any suggestions?