2013-12-13 24 views
0

我想使用自定義開發的「Auditable」屬性來跟蹤我的'POCO'對象。具有可審計屬性的對象必須跟蹤其CRUD操作。爲了做到這一點,我重寫了對象上下文的保存更改方法。但我不知道如何訪問每個實體對象的屬性值(不管屬性是否存在),在保存方法中。任何幫助表示讚賞...如何訪問EF對象上下文中的「屬性」值CurrentObjectContext_SavingChanges方法

我使用EF 6.0。

using System.ComponentModel.DataAnnotations; 
using System.ComponentModel; 
using Nido.Common.Utilities.Attributes; 

namespace DemoTest.Bll.Models 
{ 
    /// <summary> 
    /// Created by MAS IT 
    /// </summary> 
    [Auditable] 
    public class Address : BaseObject 
    { 
     // Your code here 
    } 
} 

ObjectContext的保存方法是這樣的..

private void CurrentObjectContext_SavingChanges(object sender, EventArgs e) 
{ 
// if auditable then 
// Tracking code comes here 
} 

回答

1

取決於如果你的屬性類實際上是AuditableAttribute或只是Auditable

您需要使用System.Linq的,那麼你可以做兩種:

if (sender !=null && 
    sender.GetType().GetCustomAttributes().OfType<AuditableAttribute>().Any()) 

or

if (sender !=null && 
    sender.GetType().GetCustomAttributes().OfType<Auditable>().Any())