2009-07-22 92 views
1

我想更新一個實體而不先從數據庫加載實體。ADO.net實體框架:只更新分離實體上的certian屬性

我已經完成了這一點,但只知道所有的實體屬性,然後使用「attachto」方法。

我的問題是我不希望我的應用程序需要記住所有的屬性。例如:

Dim customerEntitiy As New shopper 
customerEntitiy.shopper_id = CustomerData.CustomerID 
customerEntitiy.market_code = CustomerData.MarketSector 
customerEntitiy.email = CustomerData.Email 
customerEntitiy.modified = DateTime.Now 
context.AttachTo("shopper", customerEntitiy) 
context.SaveChanges() 

該實體還有一個「創建」字段。我不希望將這個「創建」日期一直通過我的n層應用程序。如何在保存到數據庫時「不更新」該字段?謝謝! Paul

+0

雖然可以做你想做的,它不推薦,因爲實體可能不會dierectly掛錶。例如,我使用與視圖相關的實體,並使用過程進行更新。這種方法會有非常不希望的影響... – 2009-07-23 17:28:31

回答

3

我想通了,基本上你使用了一個存根,附加它,然後只設置你想要更新的道具。實體框架只會更新已更改的內容。

Dim customerEntitiy As New commerce_shopper 
customerEntitiy.shopper_id = CustomerData.CustomerID 'this is the primary key' 
context.AttachTo("commerce_shopper", customerEntitiy) 
customerEntitiy.market_code = CustomerData.MarketSector 
customerEntitiy.email = CustomerData.Email 
customerEntitiy.modified = DateTime.Now 
context.SaveChanges() 

這將繞過「創建」日期字段。

0

我不認爲有可能使用保存更改來更新實體,而無需先從數據庫中加載它。您擁有的代碼將生成一個插入語句,而不是更新。

您可以使用存儲過程完成您正在嘗試執行的操作,該過程只更新特定的文件。

+0

我可以通過Linq到Sql來做到這一點嗎? – 2009-07-23 12:52:20

+0

我沒有使用Linq到SQL,但我認爲你會遇到同樣的問題。 – 2009-07-23 13:21:50