删除在另一个 table Entity Framework 中作为 fk 的对象
Delete an object that is a fk in another table Entity Framework
我正在尝试删除在另一个 table 中引用的对象。
我要删除的对象:
[Table("Local")]
public class Local
{
[Key]
public int Id { get; set; }
public string ViejoId { get; set; }
[Required]
[Index("UniqueNuevoId", 1, IsUnique = true)]
[Display(Name ="Nuevo Id")]
public int NuevoId { get; set; }
[Display(Name ="Id Unificado")]
public string UnificadoCon { get; set; }
[Required(ErrorMessage = "Es necesario agregar el nombre del comercio")]
[Display(Name = "Comercio")]
public string NombreComercio { get; set; }
[Display(Name = "Nom Unificado")]
public string NombreComercioUnificado { get; set; }
[Required]
public string Direccion { get; set; }
[Required]
[Display(Name ="Tel")]
public string Telefono { get; set; }
[Required]
public string Provincia { get; set; }
[Required]
public string Localidad { get; set; }
public Proveedor Proveedor { get; set; }
public Estado Estado { get; set; }
public DateTime FechaIngreso = DateTime.Today;
public bool Bonificado { get; set; }
public bool Premium { get; set; }
[Display(Name ="Instalación")]
[DataType(DataType.Date)]
public DateTime FechaInstalacion { get; set; }
public virtual List<NotasAdjuntas> notas { get; set; }
关联的对象
[Table("NotasAdjuntas")]
public class NotasAdjuntas
{
public int Id { get; set; }
[Required]
[MinLength(3)]
[MaxLength(20)]
public string Asunto { get; set; }
[Required]
public string Detalle { get; set; }
[DataType(DataType.Date)]
public DateTime Fecha { get; set; }
[DataType(DataType.Time)]
public DateTime Hora { get; set; }
public virtual Local local { get; set; }
public virtual string username { get; set; }
}
我想删除 "Local",但我知道如果我想这样做,首先我必须删除 "NotasAdjuntas"。
这是我的控制器 (LocalsController)
// GET: Locals/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Local local = db.Locales.Find(id);
if (local == null)
{
return HttpNotFound();
}
return View(local);
}
// POST: Locals/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Local local = db.Locales.Find(id);
db.Locales.Remove(local);
db.SaveChanges();
return RedirectToAction("Index");
}
感谢任何帮助!
在删除 Local 之前删除注释条目。像这样:
public ActionResult DeleteConfirmed(int id)
{
foreach (var nota in local.notas)
{
var notaParaEliminar = db.NotasAdjuntas.find(nota.Id);
db.NotasAdjuntas.Remove(notaParaEliminar);
}
Local local = db.Locales.Find(id);
db.Locales.Remove(local);
db.SaveChanges();
return RedirectToAction("Index");
}
您只需将 "local" 标记为必需的属性即可(EF 将生成正确的关系 - 在本例中为一对多)。意思是,如果您正确设置关系,它会为您自动删除 "child" 个实体。
[Table("NotasAdjuntas")]
public class NotasAdjuntas
{
public int Id { get; set; }
....
[Required] //<<<<< add this
public virtual Local local { get; set; }
....
}
我正在尝试删除在另一个 table 中引用的对象。
我要删除的对象:
[Table("Local")]
public class Local
{
[Key]
public int Id { get; set; }
public string ViejoId { get; set; }
[Required]
[Index("UniqueNuevoId", 1, IsUnique = true)]
[Display(Name ="Nuevo Id")]
public int NuevoId { get; set; }
[Display(Name ="Id Unificado")]
public string UnificadoCon { get; set; }
[Required(ErrorMessage = "Es necesario agregar el nombre del comercio")]
[Display(Name = "Comercio")]
public string NombreComercio { get; set; }
[Display(Name = "Nom Unificado")]
public string NombreComercioUnificado { get; set; }
[Required]
public string Direccion { get; set; }
[Required]
[Display(Name ="Tel")]
public string Telefono { get; set; }
[Required]
public string Provincia { get; set; }
[Required]
public string Localidad { get; set; }
public Proveedor Proveedor { get; set; }
public Estado Estado { get; set; }
public DateTime FechaIngreso = DateTime.Today;
public bool Bonificado { get; set; }
public bool Premium { get; set; }
[Display(Name ="Instalación")]
[DataType(DataType.Date)]
public DateTime FechaInstalacion { get; set; }
public virtual List<NotasAdjuntas> notas { get; set; }
关联的对象
[Table("NotasAdjuntas")]
public class NotasAdjuntas
{
public int Id { get; set; }
[Required]
[MinLength(3)]
[MaxLength(20)]
public string Asunto { get; set; }
[Required]
public string Detalle { get; set; }
[DataType(DataType.Date)]
public DateTime Fecha { get; set; }
[DataType(DataType.Time)]
public DateTime Hora { get; set; }
public virtual Local local { get; set; }
public virtual string username { get; set; }
}
我想删除 "Local",但我知道如果我想这样做,首先我必须删除 "NotasAdjuntas"。
这是我的控制器 (LocalsController)
// GET: Locals/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Local local = db.Locales.Find(id);
if (local == null)
{
return HttpNotFound();
}
return View(local);
}
// POST: Locals/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Local local = db.Locales.Find(id);
db.Locales.Remove(local);
db.SaveChanges();
return RedirectToAction("Index");
}
感谢任何帮助!
在删除 Local 之前删除注释条目。像这样:
public ActionResult DeleteConfirmed(int id)
{
foreach (var nota in local.notas)
{
var notaParaEliminar = db.NotasAdjuntas.find(nota.Id);
db.NotasAdjuntas.Remove(notaParaEliminar);
}
Local local = db.Locales.Find(id);
db.Locales.Remove(local);
db.SaveChanges();
return RedirectToAction("Index");
}
您只需将 "local" 标记为必需的属性即可(EF 将生成正确的关系 - 在本例中为一对多)。意思是,如果您正确设置关系,它会为您自动删除 "child" 个实体。
[Table("NotasAdjuntas")]
public class NotasAdjuntas
{
public int Id { get; set; }
....
[Required] //<<<<< add this
public virtual Local local { get; set; }
....
}