0

我目前正在从事一个项目,该项目的要求是记录对实体的每个单独属性的更改。许多实体与查找值类型具有一对多的关系,当这种变化时,我可以跟踪到,即“1”已更改为“2”。我想要完成的是将数字转换为更具可读性的值。

我有下面的代码,我认为它可以工作,但它没有:(

namespace DomainClasses
{
    public interface IAuditDescribable
    {
        String GetAuditDescription();
    }
    public abstract class BaseEntity
    {
        public Int32 Id { get; set; }
    }
    public class Car : BaseEntity
    {
        public String Vrn { get; set; }
        public Int32? TransmissionId { get; set; }
        public virtual Transmission Transmission { get; set; }
    }
    public class Transmission : BaseEntity, IAuditDescribable
    {
        public String Name { get; set; }
        public String GetAuditDescription()
        {
            return Name;
        }
    }
}


namespace AuditTrailTwo
{
    public class AuditContext : DbContext
    {    
        public DbSet<Car> Cars { get; set; }
        public DbSet<Transmission> Transmissions { get; set; }

        public override int SaveChanges()
        {
            ChangeTracker.DetectChanges();

            var octx = ((IObjectContextAdapter)this).ObjectContext;
            var changes = octx.ObjectStateManager.GetObjectStateEntries(EntityState.Modified);

            foreach (var change in changes)
            {
                var changedProperties = change.GetModifiedProperties();
                var associationSets = change.EntitySet.EntityContainer.AssociationSets;

                foreach (var changedProperty in changedProperties)
                {
                    CurrentValueRecord current = change.CurrentValues;
                    var newValue = current.GetValue(
                        current.GetOrdinal(changedProperty));

                    DbDataRecord original = change.OriginalValues;
                    var oldValue = original.GetValue(
                        original.GetOrdinal(changedProperty));

                    var aSet = associationSets
                        .Where(x => x.ElementType.IsForeignKey)
                        .FirstOrDefault(x => x.ElementType.Constraint.ToProperties[0].Name == changedProperty);

                    if (aSet != null)
                    {
                        var targetEnd = aSet.AssociationSetEnds.GetValue(aSet.Name+"_Target", true);


                        //This type is always null because the 'FullName' returns: 'AuditTrailTwo.Transmission'
                        //and not 'DomainClasses.Transmission' as I was expecting.
                        var type = Type.GetType(targetEnd.EntitySet.ElementType.FullName);

                        if (type != null)
                        {
                            var typeSet = Set(type);

                            var newEntry = typeSet.Find(newValue) as IAuditDescribable;
                            var newEntryValue = newEntry.GetAuditDescription();

                            var oldEntry = typeSet.Find(oldValue) as IAuditDescribable;
                            var oldEntryValue = oldEntry.GetAuditDescription(); 
                        }
                    }
                }
            }
            return base.SaveChanges();
        }
    }
}

我看过许多讨论使用实体框架进行审计的帖子,但到目前为止我还没有找到任何方法来实现我想要做的事情。我希望这仅仅是因为我忽略了一些东西。

任何帮助表示赞赏。

4

1 回答 1

0

您可以使用面向方面的编程来处理属性发生变化的情况。 http://www.postsharp.net/

于 2014-01-10T01:51:05.920 回答