none
Self tracking Entities partial OnContextCreated method doesn't fire SavingChanges RRS feed

  • Question

  • i would like to introduce Audit trailing component into a project. i am using self tracking entities. i already added partial void OnContextCreated() method in context.tt file as well as in the Context.cs constructor after the Initialize() method. i also write a partial class for handling business logic where i implemented my partial OnContextCreated() method and registered SavingChanges event. i setup a break point on the OnContextCreated() method but unfortunately it never trigger the SavingChanges event. please have a look at the following three files.

       

    1. Context.tt
    2. Context.cs
    3. Partial class for business logic


    Context.tt

    partial void OnContextCreated();

                                                                                                           




    Context.cs


    using Domain.Core.Entities;
        using Domain.Core;
        using Domain.MainModule.Entities.Objects;
        using System.Reflection;
        
        namespace Infrastructure.Data.MainModule.UnitOfWork
        {
            [System.Diagnostics.DebuggerNonUserCode()]
            public partial class MainModuleUnitOfWork : ObjectContext,IMainModuleUnitOfWork
            {
                public const string ConnectionString = "name=MainModuleUnitOfWork";
                public const string ContainerName = "MainModuleUnitOfWork";
            
                #region Constructors
            	
                public MainModuleUnitOfWork()
                    : base(ConnectionString, ContainerName)
                {
                    Initialize();
            		OnContextCreated();
                }
            
                public MainModuleUnitOfWork(string connectionString)
                    : base(connectionString, ContainerName)
                {
                    Initialize();
            		OnContextCreated();
                }
            
                public MainModuleUnitOfWork(EntityConnection connection)
                    : base(connection, ContainerName)
                {
                    Initialize();
            		OnContextCreated();
                }
            
                private void Initialize()
                {
                    // Creating proxies requires the use of the ProxyDataContractResolver and
                    // may allow lazy loading which can expand the loaded graph during serialization.
                    ContextOptions.ProxyCreationEnabled = false;
            		ContextOptions.LazyLoadingEnabled = false;
                    ObjectMaterialized += new ObjectMaterializedEventHandler(HandleObjectMaterialized);
            
            		OnContextCreated();
                }
            
            	partial void OnContextCreated();
          }
    }
    




    Partial class for business logic

        using System.Reflection;
        using System.Xml;
        using System.Runtime.Serialization;
        using System.IO;
        using Domain.MainModule.Entities.Objects;
        using Infrastructure.Data.MainModule.UnitOfWork;


        namespace Infrastructure.Data.MainModule.UnitOfWork
        {
            public partial class MainModuleUnitOfWork
            {
                //public string UserName { get; set; }

                private string UserName = "Rusho";

                List<DbAudit> auditTrailList = new List<DbAudit>();

                public enum AuditActions
                {
                    I,
                    U,
                    D
                }


                partial void OnContextCreated()
                {
                    SavingChanges +=new EventHandler(MainModuleUnitOfWork_SavingChanges);
                    //this.SavingChanges += new EventHandler(MainModuleUnitOfWork_SavingChanges);
                }

                private void MainModuleUnitOfWork_SavingChanges(object sender, EventArgs e)
                {
                    this.DetectChanges();
                    //IEnumerable<ObjectStateEntry> changes = this.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified);
                    IEnumerable<ObjectStateEntry> changes = this.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted | EntityState.Modified);
                    foreach (ObjectStateEntry stateEntryEntity in changes)
                    {
                        if (!stateEntryEntity.IsRelationship &&
                                stateEntryEntity.Entity != null &&
                                    !(stateEntryEntity.Entity is DbAudit))
                        {//is a normal entry, not a relationship
                            DbAudit audit = this.AuditTrailFactory(stateEntryEntity, UserName);
                            auditTrailList.Add(audit);
                        }
                    }

                    if (auditTrailList.Count > 0)
                    {
                        foreach (var audit in auditTrailList)
                        {//add all audits
                            this.DbAudits.AddObject(audit);// this.AddToDBAudit(audit);
                        }
                    }
                }

    Partial class for business logic

       

    using System.Reflection;
        using System.Xml;
        using System.Runtime.Serialization;
        using System.IO;
        using Domain.MainModule.Entities.Objects;
        using Infrastructure.Data.MainModule.UnitOfWork;
        
        
        namespace Infrastructure.Data.MainModule.UnitOfWork
        {
            public partial class MainModuleUnitOfWork
            {
                //public string UserName { get; set; }
        
                private string UserName = "Rusho";
        
                List<DbAudit> auditTrailList = new List<DbAudit>();
        
                public enum AuditActions
                {
                    I,
                    U,
                    D
                }
        
        
                partial void OnContextCreated()
                {
                    SavingChanges +=new EventHandler(MainModuleUnitOfWork_SavingChanges);
                    //this.SavingChanges += new EventHandler(MainModuleUnitOfWork_SavingChanges);
                }
        
                private void MainModuleUnitOfWork_SavingChanges(object sender, EventArgs e)
                {
                    this.DetectChanges();
                    //IEnumerable<ObjectStateEntry> changes = this.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified);
                    IEnumerable<ObjectStateEntry> changes = this.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted | EntityState.Modified);
                    foreach (ObjectStateEntry stateEntryEntity in changes)
                    {
                        if (!stateEntryEntity.IsRelationship &&
                                stateEntryEntity.Entity != null &&
                                    !(stateEntryEntity.Entity is DbAudit))
                        {//is a normal entry, not a relationship
                            DbAudit audit = this.AuditTrailFactory(stateEntryEntity, UserName);
                            auditTrailList.Add(audit);
                        }
                    }
        
                    if (auditTrailList.Count > 0)
                    {
                        foreach (var audit in auditTrailList)
                        {//add all audits 
                            this.DbAudits.AddObject(audit);// this.AddToDBAudit(audit);
                        }
                    }
                }
    

    Tuesday, May 1, 2012 2:36 PM

All replies