2017-06-29 33 views
0

我已經添加了一個道具給我的班級並添加了一個新的DbSet但保存時,它不存儲我的子對象。 我的房子類:實體框架6不存儲子對象

[Table("Houses")] 
public class House 
{ 
    [Key] 
    public int ID { get; set; } 
    public List<Price> Prices { get; set; } // <-- new prop 
} 

我的DbContext現在有一個Prices道具:public DbSet<Price> Prices { get; set; }我啓用遷移,增加了遷移和更新數據庫。所以價格表已創建。

當我更新House對象時,它不會在Prices表中插入任何內容。

var h = new House(); // with prices etc filled 
if (db.Houses.Any(hc => hc.Code.Equals(h.Code, StringComparison.InvariantCultureIgnoreCase))) 
{ 
    var original = db.Houses.First(k => k.Code.Equals(h.Code, StringComparison.InvariantCultureIgnoreCase)); 
    h.ID = original.ID; // workaround for The property 'ID' is part of the object's key information and cannot be modified. 
    h.CountryId = original.CountryId; 

    db.Entry(original).CurrentValues.SetValues(h); 
    db.SaveChanges(); // does update House fields, but no prices 
} else { // add new 
    db.Houses.Add(h); 
    db.SaveChanges(); 
} 

我確實添加了public virtual House House { get; set; }Price類。但我沒有填充它,因爲填充房屋對象時,我不知道數據庫中的ID。也許這是造成它?我也看過https://stackoverflow.com/a/26572122/169714,並將此我Price類:在價格表

[ForeignKey("HouseId")] 
public virtual House House { get; set; } 
public int HouseId { get; set; } 

,但仍然沒有條目。 我可能做錯了存儲/更新數據庫。

編輯當前存儲方法:

using (var db = new MyCommon.HouseContext()) 
{ 
    var l = db.Countries.First(tmpC => tmpC.Code.Equals(h.Country.Code)); 
    h.OperatorId = op.ID; 
    h.CountryId = l.ID; 
    h.Country = l; 

    var existingHouse = db.Houses.Where(p => p.Code.Equals(h.Code, StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault(); 

    if (existingHouse != null) 
    { 
     // update 
     h.ID = existingHouse.ID; // workaround for The property 'ID' is part of the object's key information and cannot be modified. 
     h.CountryId = existingHouse.CountryId; 
     h.OperatorId = existingHouse.OperatorId; 

     db.Entry(existingHouse).CurrentValues.SetValues(h); 
     db.Entry(existingHouse).State = System.Data.Entity.EntityState.Modified; 
     //db.SaveChanges(); // moved to bottom for perf. 
    } 
    else 
    { 
     existingHouse = h; 
     db.Houses.Add(h);   // insert 
    } 

    foreach (var ft in existingHouse.Prices) 
    { 
     var existingFt = existingHouse.Prices.SingleOrDefault(f => f.ID == ft.ID); 

     if (existingFt != null) 
     { 
      db.Entry(existingFt).CurrentValues.SetValues(ft); 
      db.Entry(existingFt).State = System.Data.Entity.EntityState.Modified; 
     } 
     else 
     { 
      existingHouse.Prices.Add(ft); 
     } 
    } 
    db.SaveChanges(); 
} 
+0

您正在更新House對象,但是您在什麼時候對需要更新的價格進行了更改?應該自動處理,但很難說,因爲你省略了那部分。 –

+0

的確如此,但它在'if'之前。我在那裏填寫價格。調試器顯示h.Prices中有超過100個對象。 Db表格保持空白。 –

+0

你是否標記了他們的EntityState.Modified(或'Added')? –

回答

0

檢查對象的EntityState。將該對象附加到您的上下文中,並遍歷這些Prices以標記EntityState.Modified。如果Price對象是新對象,則可以使用EntityState.Added.您可以看到here用於'upsert'模式示例。

+0

我正在調查https://stackoverflow.com/a/27177623/169714並將其與您的答案相結合。 MSDN鏈接真的很有用。會讓你知道它是否已修復!謝謝! –

+0

我已閱讀msdn文章和我在之前的評論中發佈的SO問題。結合https://stackoverflow.com/questions/5940225/fastest-way-of-inserting-in-entity-framework?rq=1我以爲我編輯的問題應該工作。因爲房屋對象被跟蹤並且兒童應該也被追蹤和插入/更新。但沒有行觸摸或插入數據庫。我錯過了一些東西...... –