2017-05-08 54 views
1

我試圖刪除在另一個表中引用的對象。刪除在另一個表中是fk的對象實體框架

對象我想刪除:

[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; } 

} 

我想刪除一個「本地」的對象,但我明白,如果我想這樣做,首先我必須擺脫「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"); 
    } 

任何幫助表示讚賞!

+0

您是否在嘗試刪除_Local_條目時收到異常? –

回答

1

你只需要標記「本地」有需要的屬性,應該做的(EF會產生適當的關係 - 一個一對多在這種情況下)。意思是,如果你只是正確地建立關係,它會爲你自動刪除「子」實體。

[Table("NotasAdjuntas")] 
public class NotasAdjuntas 
{ 
    public int Id { get; set; } 
    .... 
    [Required] //<<<<< add this 
    public virtual Local local { get; set; } 
    .... 
} 
+0

像一個魅力工作,謝謝! – EmiL

0

刪除筆記條目,然後刪除本地。事情是這樣的:

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"); 
}