覆盖不使用dbcontext的插件

时间:2017-02-01 08:58:25

标签: c# asp.net-mvc plugins entity-framework-6

我有一个访问dbcontext的类。我试图覆盖我的NopContextOverride中的nopObjectContext

public class NopObjectContext : DbContext, IDbContext

        {
            #region Ctor

            public NopObjectContext(string nameOrConnectionString)
                : base(nameOrConnectionString)
            {
                //((IObjectContextAdapter) this).ObjectContext.ContextOptions.LazyLoadingEnabled = true;
            }
    }

    I am overriding this class in my plugin


     public class NopContextOverride : NopObjectContext
        {
            public NopContextOverride(string nameOrConnectionString) : base(nameOrConnectionString)
            {
            }
            public override int SaveChanges()
            {
                this.Configuration.AutoDetectChangesEnabled = false;


                this.Configuration.LazyLoadingEnabled = false;
                this.Configuration.ProxyCreationEnabled = false;

                var modifiedEntities = ChangeTracker.Entries()
                    .Where(e => e.State == EntityState.Added
                                                  || e.State == EntityState.Modified
                                                  || e.State == EntityState.Deleted).ToList();

                foreach (var change in modifiedEntities)
                {
                    var entityName = change.Entity.GetType().Name;
                    //var primaryKey = GetPrimaryKeyValue(change);

                    List<Nop.Plugin.XcellenceIt.ChangeTracker.Domain.ChangeTracker> changeTrackerList = new List<Nop.Plugin.XcellenceIt.ChangeTracker.Domain.ChangeTracker>();

                    foreach (var prop in change.OriginalValues.PropertyNames)
                    {
                        var originalValue = change.OriginalValues[prop].ToString();
                        var currentValue = change.CurrentValues[prop].ToString();
                        if (originalValue != currentValue)
                        {
                            Nop.Plugin.XcellenceIt.ChangeTracker.Domain.ChangeTracker changeTracker = new Nop.Plugin.XcellenceIt.ChangeTracker.Domain.ChangeTracker()
                            {
                                EntityName = entityName,
                                //PrimaryKeyValue = primaryKey.ToString(),
                                FieldName = prop,
                                OldValue = originalValue,
                                NewValue = currentValue,
                                EntityState = Convert.ToString(change.State),
                                DateChanged = DateTime.UtcNow
                            };
                            changeTrackerList.Add(changeTracker);
                        }
                    }
                }
                this.Configuration.AutoDetectChangesEnabled = true;
                return base.SaveChanges();
            }
        }

覆盖不起作用。我还解决了NopObjectContext在依赖注册中的依赖性。但是它还没有工作。任何人都可以告诉我我做错了什么。

0 个答案:

没有答案