2015-05-26 15 views
1

我有一個客戶,我想在存儲庫中進行更新。更新實體但不是空值屬性

var customer = new Customer{ Name = "Test" } 

客戶還有更多屬性爲空,因爲我之前沒有將它們加載到客戶端。因此這些屬性的所有默認值如null or 0

與最新的EF 6有什麼關係,即只有屬性名稱已更新且來自客戶的其他屬性未被覆蓋?

1.)我該如何查詢/更新客戶?

2.)如果客戶有一個集合和他已經改變了某些會議的某些屬性 - 但不是所有的屬性 - 會怎麼會是覆蓋行爲?

UPDATE僞代碼

Open context 

Get customer 

Close context 


Open context 

Update customer.name 

SAveChanges 

Close context 

的custom.name不會被保存,爲什麼?

回答

0

假設您已經生成了您的模型並且名稱爲「MyEntities」,並且您應該有一個客戶ID(如果您正在更新現有的)。這是一個使用C#的解決方案。

using(MyEntities db = new MyEntities()) { 
    //this will retreieve the customer based on ID 
    Customer cust = db.Customers.FirstOrDefault(c => c.CustomerID == custID); 
    //you can update each column 
    cust.Name = "Test"; 

    //save the changes to the entity 
    db.SaveChanges(); 
} 
+0

當我這樣做,那麼客戶不更新。問題可能是客戶在另一個環境中被檢索到。此上下文已關閉。然後在新的情況下,我更新了customer.Name。但在這裏它不會更新。我是否需要調用任何附加代碼? – Pascal

+0

我用更多信息更新了我的問題。 – Pascal

+0

@帕斯卡這應該工作,你無法使用這種方法保存更新的屬性? – Yasser

相關問題