2010-11-14 89 views
0

編輯:更重要的是,我這樣做是沒有錯誤和沒有記錄插入:實體框架4:創建一個新的實體,保存它,沒有錯誤,並沒有插入記錄?

var myTopLevelEntity = _DC.TopLevelEntity.Where(p => p.ID = 1).First(); 
var newEntity = new Entity(); 
newEntity.pocofield = ""; // set all primary keys and poco fields 
newEntity.TopLevelEntity = myTopLevelEntity; 
_DC.Entitys.Add(newEntity); 

我檢查SQL服務器的探查。在此期間沒有發生除選擇之外的命令。

(END EDIT)

我有一個實體的集合一次添加,所有新。他們依賴於另一個表中的記錄。我做到以下幾點:

var myTopLevelEntity = _DC.TopLevelEntity.Where(p => p.ID = 1).First(); 

NewEntity都具有層次:

newEntity.A[].pocofields 
newEntity.B[].pocofields 
newEntity.C[].pocofields 
newEntity.C[].D[].pocofields 

所以,我做這個

var newEntity = new Entity(); 
newEntity.pocofield = ""; // various poco fields, set all keys 

var A = new AEntity(); 
A.pocofield = ""; // various poco fields, set all keys 
newEntity.As.Add(A); 

var B = new BEntity(); 
B.pocofield = ""; // various pocos, set all keys 
newEntity.Bs.Add(B); 

var C = new CEntity(); 
C.pocofield = ""; // various, set all keys 
var D = new DEntity(); 
D.pocofield = ""; // various, set all keys 
C.Ds.Add(D); 
newEntity.Cs.Add(C); 

newEntity.TopLevelEntitys.Add(myTopLevelEntity); 
_DC.Entitys.Add(newEntity); 

在這一點上,沒有出現錯誤(我沒有try/catch任何地方)。數據庫中也沒有記錄。我在做什麼不正確?

回答

4

我想你忘了撥打電話_DC.SaveChanges

+0

OMG說一個心靈麻木的經歷大聲笑。 – 2010-11-14 18:17:38