Entität Framework und DatenbankC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Entität Framework und Datenbank

Post by Anonymous »

Ich arbeite an einem EF -Projekt. Dies ist meine Entität: < /p>
namespace AnagraficaEF.DA
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;

[Table("Anagrafica")]
public partial class Anagrafica
{

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

[StringLength(50)]
public string Nome { get; set; }

[StringLength(50)]
public string Cognome { get; set; }

[StringLength(2)]
public string Sesso { get; set; }
}
}
< /code>
Und dies ist mein Controller: < /p>
using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AnagraficaEF.DA;

namespace AnagraficaEFTest.Controllers
{
public class AnagraficaController : Controller
{
public ActionResult Lista()
{
ContestoDB _context = new ContestoDB();

IQueryable _query = from x in _context.Anagrafica select x;

List _list = _query.ToList();

return View(_list);
}

public ActionResult Dettaglio(int id)
{
ContestoDB _contesto = new ContestoDB();

IQueryable _query = from x in _contesto.Anagrafica
where x.IdAnagrafica == id
select x;

Anagrafica _return = _query.SingleOrDefault();

return View(_return);
}

public ActionResult FormInserimentoUtente()
{
return View();
}

public ActionResult Aggiungi(Anagrafica _entity)
{
ContestoDB _contesto = new ContestoDB();

_contesto.Anagrafica.Add( _entity );

_contesto.SaveChanges();

IQueryable result = from x in _contesto.Anagrafica select x;

List _lista = result.ToList();

return View(result);
}

public ActionResult FormModifica()
{
return View();
}

public ActionResult Delete(int id)
{
ContestoDB _cont = new ContestoDB();

Anagrafica temp = new Anagrafica() { IdAnagrafica = id};

_cont.Anagrafica.Attach(temp);

_cont.Anagrafica.Remove(temp);

_cont.SaveChanges();

IQueryable result = from x in _cont.Anagrafica select x;

List _lista = result.ToList();

return RedirectToAction("Lista");
}

public ActionResult Aggiorna(Anagrafica _entity)
{
ContestoDB _con = new ContestoDB();

_con.Entry(_entity).State = System.Data.Entity.EntityState.Modified;

_con.SaveChanges();

IQueryable result = from x in _con.Anagrafica select x;

List _lista = result.ToList();

return View(result);
}
}
}
< /code>
Dies ist meine modifische Ansicht: < /p>
@model AnagraficaEF.DA.Anagrafica
@{
ViewBag.Title = "FormModifica";
}

Form Modifica

@using (Html.BeginForm("Aggiorna", "Anagrafica", FormMethod.Post, new { enctype = "multipart/form-data" }))
{

@Html.DisplayFor(m => m.IdAnagrafica)

@Html.LabelFor(m => m.Nome)
@Html.TextBoxFor(m => m.Nome, new { @class = "form-control" })

@Html.LabelFor(m => m.Cognome)
@Html.TextBoxFor(m => m.Cognome, new { @class = "form-control" })

@Html.LabelFor(m => m.Sesso)
@Html.TextBoxFor(m => m.Sesso, new { @class = "form-control" })

Salva

}
< /code>
Wenn ich auf die Schaltfläche "salva" klicke, speichert der DBContext die Änderungen nicht. Wie kann ich dieses Problem lösen? Die "Aggiorna" -Methode ist die einzige, die nicht funktioniert! Die anderen Methoden funktionieren sehr gut. Ich kann eine neue Anagrafica erstellen, ich kann sie löschen, ich kann Anagraficas Liste und ich kann Anagrafica Details lesen, aber ich kann Anagrafica nicht aktualisieren.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post