使用EF4.0创建审核记录

时间:2014-01-03 09:54:47

标签: c# winforms entity-framework

我正在尝试为历史目的创建审核记录,我从以下文章中找到了此代码。

List<sysaudit> auditTrailList = new List<sysaudit>();
            IEnumerable<ObjectStateEntry> changes =  ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified);
            foreach (ObjectStateEntry stateEntryEntity in changes)
            {
                if (!stateEntryEntity.IsRelationship &&
                        stateEntryEntity.Entity != null &&
                            !(stateEntryEntity.Entity is sysaudit))
                {//is a normal entry, not a relationship
                    sysaudit audit = pamsContext.AuditTrailFactory(stateEntryEntity, "");
                    auditTrailList.Add(audit);
                }
            }
            if (auditTrailList.Count > 0)
            {
                foreach (var audit in auditTrailList)
                {//add all audits
                    this.AddToDBAudit(audit);
                }
            }

它抱怨非静态字段方法或属性实体状态需要对象引用,但我已经在代码中引用了这是我的using子句。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    using System.IO;
    using System.Data.Objects;
    using System.Diagnostics;
    using System.Text.RegularExpressions;

这是抱怨的行

IEnumerable<ObjectStateEntry> changes = ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified);

1 个答案:

答案 0 :(得分:0)

该行上的EntityState应引用System.Data.EntityState枚举。你可以将鼠标悬停在EntityState的一个提及上来验证这个吗?

鉴于您提到的编译错误,我怀疑您的类还包含一个名为EntityState的非静态字段或属性。

如果是这样,您需要更改相关行以完全限定命名空间,即:

IEnumerable<ObjectStateEntry> changes = ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Added | System.Data.EntityState.Deleted | System.Data.EntityState.Modified);