我真的很努力地將我的項目上的所有內容都與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對象」。我真的覺得這與業主 - 孩子班級之間的雙向參考有關,但仍然不知道發生了什麼以及如何解決它。
我開始覺得真的迷失在這裏。任何想法都將非常感激。由於