2013-12-13 16 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())