8
A
回答
7
根據上下文的類型和生成的實體,您可以通過幾種不同的方式來完成。 對於從實體或POCO繼承的對象,您可以使用ObjectStateManager
在自我跟蹤實體的情況下,您可以從實體本身使用跟蹤器。
請提供您如何產生的上下文的方式更多的細節和如何做變更
編輯(2): 可以查詢ObjectStateManager
對改變後的條目只是這樣的:
var changed = ctx.ObjectStateManager.GetObjectStateEntries().Where(e=>e.State != EntityState.Unchanged);
編輯( 1):
以下示例從MSDN演示如何查詢更改:
int orderId = 43680;
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
var order = (from o in context.SalesOrderHeaders
where o.SalesOrderID == orderId
select o).First();
// Get ObjectStateEntry from EntityKey.
ObjectStateEntry stateEntry =
context.ObjectStateManager
.GetObjectStateEntry(((IEntityWithKey)order).EntityKey);
//Get the current value of SalesOrderHeader.PurchaseOrderNumber.
CurrentValueRecord rec1 = stateEntry.CurrentValues;
string oldPurchaseOrderNumber =
(string)rec1.GetValue(rec1.GetOrdinal("PurchaseOrderNumber"));
//Change the value.
order.PurchaseOrderNumber = "12345";
string newPurchaseOrderNumber =
(string)rec1.GetValue(rec1.GetOrdinal("PurchaseOrderNumber"));
// Get the modified properties.
IEnumerable<string> modifiedFields = stateEntry.GetModifiedProperties();
foreach (string s in modifiedFields)
Console.WriteLine("Modified field name: {0}\n Old Value: {1}\n New Value: {2}",
s, oldPurchaseOrderNumber, newPurchaseOrderNumber);
// Get the Entity that is associated with this ObjectStateEntry.
SalesOrderHeader associatedEnity = (SalesOrderHeader)stateEntry.Entity;
Console.WriteLine("Associated Enity's ID: {0}", associatedEnity.SalesOrderID);
}
2
通過使用數據庫結構本身通常是一種很好的做法。 這只是您現在擁有的另一種方法。
您可以在名爲ModifiedOn
的表中創建類型爲datetime
的新字段,並在每次更新數據庫中的行時對其進行更新。
然後當你要更改的行的具體時間後,你只需使用:
where ModifiedOn > dateTime
這只是你如何能夠從不同的角度解決這個問題的另一個建議。
相關問題
- 1. 在EF Core中添加新實體後如何更新實體
- 2. 更新實體EF
- 3. 如何更新EF 4.0中的非標量實體屬性?
- 4. 如何在EF中正確更新模型實體實例?
- 5. 在SaveChanges後獲取新增實體?
- 6. EF如何更新包含實體列表的實體
- 7. 更改WPF RichTextBox的字體增量
- 8. 如何在EF中創建另一個實體的新實體?
- 9. 實體數量不會更新,直到實體獲取
- 10. EF修改函數中的實體
- 11. 獲取實體,其狀態增加了
- 12. 如何更改窗體中的實例中的變量的值
- 13. 如何在nhibernate中使用其實體屬性獲取實體?
- 14. 如何獲取父實體?
- 15. 更改EF模型並在運行時創建新實體
- 16. mysql增量更改
- 17. 如何獲取我的數據庫第一個EF 6.1實體成爲更改跟蹤代理
- 18. 如何將現有實體添加到EF中的新實體?
- 19. 如何更新EF CF中的實體對象(分離)?
- 20. 如何從Nhibernate會話中獲取已更改(髒)實體的列表?
- 21. 從NHibernate會話中獲取已更改的實體
- 22. EF更新兩個實體不在數據庫中更新
- 23. 增量查詢用戶資源:如何獲取僅更改的屬性
- 24. 獲取實體框架以在更新時從數據庫獲取實體
- 25. Symfony2在另一個實體中獲取實體的實體
- 26. 如何獲取實體框架中插入實體的ID?
- 27. EF的DbContext更新實體集合
- 28. 更新N個已加入EF實體
- 29. EF代碼第一:更新實體
- 30. EF實體不會更新數據庫
使用嚮導從db生成模型,典型代碼repo.entity.value = newvalue進行更改 – Kumar 2011-12-14 18:02:47
請參閱擴展答案 – vittore 2011-12-14 19:36:50