2012-11-12 20 views
0

我真的很努力地將我的項目上的所有內容都與EF一起工作,但它確實變得越來越困難,有時它讓我懷疑它是否真的是明智之舉在EF上反對編碼數據庫的所有內容)。 那麼,我的問題仍然與1-N創建/編輯/刪除功能有關(應該很簡單,對吧?)。EntityFramework和ont-to-many增刪改查操作

好的,我在這裏粘貼一些簡單的代碼。

對於實體,我得到了主類爲:

[Table("OLIM_LOTE")] 
public class Lote 
{ 
    [Key] 
    [Column("LOTE_ID_LOTE")] 
    public int? IDLote { get; set; } 

    [Column("LOTE_TX_OBS")] 
    public string Obs {get;set;} 

    [Column("LOTE_TX_DOCUMENTO_EXTRA")] 
    public string DocumentoExtra { get; set; } 

    [NotMapped] 
    public List<DocumentoLote> Documentos { get; set; } 

    public void LoadLists() 
    { 
     OlimpiqueDBContext myDbContext = new OlimpiqueDBContext(); 
     var docs = (from doc in myDbContext.DocumentosLote 
        where doc.IDLote == this.IDLote 
        select doc); 
     this.Documentos = docs.ToList<DocumentoLote>(); 
    } 

} 

[請注意,我用的可空INT?對於關鍵 - 否則它拋出我驗證異常,要求在創建]

值對於孩子上課,我得到這個:

[Table("OLIM_DOCUMENTO_LOTE")] 
public class DocumentoLote 
{ 
    [Key] 
    [Column("DOLO_ID_DOCUMENTO_LOTE")] 
    public int? IDDocumentoLote { get; set; } 

    [Column("DOCU_ID_DOCUMENTO")] 
    [ForeignKey("Documento")] 
    public int IDDocumento { get; set; } 

    public virtual Documento Documento { get; set; } 

    [Column("LOTE_ID_LOTE")] 
    [ForeignKey("Lote")] 
    public int IDLote { get; set; } 

    public virtual Lote Lote { get; set; } 
} 

[注意子類有一個參考還給失主類,它是「IDLote」和「Lote」屬性,並且所有者類具有子類實例列表 - 所以我得到了我的雙向引用 - 我認爲這與問題有某種關係]

I獲得了由VS2012自動生成的與Lote類相關的讀/寫功能的Controller和View。 我在View中所做的事情可以描述爲:我使用Jquery DataTable來管理子類數據(用戶可以在DataTable上添加「N」個實例)。我用一個JS方法調用了Post Button,它簡單地從Form和DataTable獲取所有數據,並將其包裝在JSon對象中,並通過Ajax將其發送給控制器。

接收它可以如下簡化控制器方法:

[HttpPost] 
    public JsonResult Edit(Lote lote) 
    { 
     try 
     { 
      if (ModelState.IsValid) //<< HAVING PROBLEMS HERE... DETAILS BELOW 
      { 
       if (lote.IDLote.HasValue) 
       { 
        //Separete updates/inserts from deletes 
        List<int?> dbDocs = db.DocumentosLote 
            .Where(dt => dt.IDLote == lote.IDLote) 
            .Select(dt => dt.IDDocumentoLote) 
            .ToList(); 

        List<int?> postedDocs = lote.Documentos 
         .Select(pt => pt.IDDocumentoLote) 
         .ToList(); 

        List<int?> deletedDocs = dbDocs 
         .Except(postedDocs).ToList(); 

        //Perform deletes 
        foreach (var delDocId in deletedDocs) 
        { 
         if (delDocId.HasValue) 
         { 
          DocumentoLote delDoc = db.DocumentosLote 
           .Where(dt => dt.IDLote == lote.IDLote && dt.IDDocumentoLote == delDocId) 
           .Single(); 

          db.Entry(delDoc).State = EntityState.Deleted; 
         } 
        } 

        //Perform insert and updates 
        foreach (var doc in lote.Documentos) 
        { 
         if (doc.IDDocumentoLote.HasValue) 
         { 
          db.Entry(doc).State = EntityState.Modified; 
         } 
         else 
         { 
          db.Entry(doc).State = EntityState.Added; 
          doc.IDLote = (int)lote.IDLote; 
         } 
        } 
       } 
       else 
       { 
        db.Lotes.Add(lote); 
       } 
       db.SaveChanges(); 

       // If Sucess== 1 then Save/Update Successfull else there it has Exception 
       return Json(new { Success = 1, ex = "" }); 
      } 
      else 
      { 
       return Json(new { Success = 0, ex = "Falha ao tentar salvar os dados" }); 
      } 
     } 
     catch (Exception ex) 
     { 
      // If Sucess== 0 then Unable to perform Save/Update Operation and send Exception to View as JSON 
      return Json(new { Success = 0, ex = ex.Message.ToString() }); 
     } 
    } 

問題:嗯,我經歷了很多真正傳遞到了這一點,現在,我只拿到了2個問題。首先是創建拋出一個驗證異常,說它需要一個IDLote(對於子類 - 但無論如何,如果擁有者類本身在創建時還沒有創建Id時,我將如何獲得它)? 第二個問題:刪除根本不起作用!無論我如何編碼,它都會引發異常「無法定義對象,因爲它們連接到不同的ObjectContext對象」。我真的覺得這與業主 - 孩子班級之間的雙向參考有關,但仍然不知道發生了什麼以及如何解決它。

我開始覺得真的迷失在這裏。任何想法都將非常感激。由於

回答

0

由於沒有對這個老問題有很多的觀點,現在我有一些答案,我張貼供參考:

Q - 關於int?類型的關鍵屬性: A - 它不一定是一個空的int。實體可以用一個簡單的int屬性作爲鍵來聲明,當從視圖發回JSon對象時,回到某個控制器方法,該屬性(鍵)可以用值「0」填充。一旦EF持續存在,EF將生成正確的值。

Q - 關於導航屬性以及如何實現這兩個類之間的關係,當它們中的任何一個在這些關鍵字上都不具有值(非零)時: A - 要發回的JSon對象可以實現它們之間的確切的導航關係。在控制器綁定發佈到它應該接收的模型的數據後,它將「理解」它們的關係,並且一旦生成了這些鍵的值,它們將正確地引用另一個。

Q - 關於上的刪除方法的嘗試中描述的錯誤: A - 當對象應該與其他對象交互,和這些交互應保持或以任何方式「理解」,由EF,必須已經獲得它們,生成或附加到同一個DBContext。 EF依靠數據庫上下文來創建這種交互的樹,因此,當objets不存在於相同的數據庫上下文時,無法構建此樹。