public override int SaveChanges() { DoBusinessRules(); ObjectContext ctx = ((IObjectContextAdapter)this).ObjectContext; List <ObjectStateEntry> objectStateEntryList = ctx.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Deleted) .ToList(); EntityState state = new EntityState(); ObjectStateEntry objectStateEntry = null; if (objectStateEntryList.Count > 0) { objectStateEntry = objectStateEntryList.Where(p => p.Entity is BusinessRule).ToList()[0]; state = objectStateEntry.State; } SetDisplayValue(objectStateEntryList, state); if (objectStateEntry != null && state == EntityState.Modified) { var id = objectStateEntry.EntityKey.EntityKeyValues[0].Value.ToString(); MakeUpdateJournalEntry(id, objectStateEntry.State.ToString(), objectStateEntryList.Where(p => p.Entity is BusinessRule).ToList()[0]); } if (objectStateEntry != null && state == EntityState.Deleted) { var id = objectStateEntry.EntityKey.EntityKeyValues[0].Value.ToString(); ConditionContext brconditions = new ConditionContext(); long? longid = Convert.ToInt64(id); var condlist = brconditions.Conditions.Where(p => p.RuleConditionsID.Value == longid); foreach (var cond in condlist) { brconditions.Conditions.Remove(cond); } brconditions.SaveChanges(); RuleActionContext brActions = new RuleActionContext(); ActionArgsContext brArgs = new ActionArgsContext(); var actionlist = brActions.RuleActions.Where(p => p.RuleActionID.Value == longid); foreach (var action in actionlist) { brActions.RuleActions.Remove(action); var argslist = brArgs.ActionArgss.Where(p => p.ActionArgumentsID == action.Id); foreach (var args in argslist) { brArgs.ActionArgss.Remove(args); } brArgs.SaveChanges(); } brActions.SaveChanges(); } var result = base.SaveChanges(); if (objectStateEntryList.Count > 0) { if (objectStateEntry != null && state == EntityState.Added) { var id = objectStateEntry.EntityKey.EntityKeyValues[0].Value.ToString(); MakeAddJournalEntry(id, state.ToString(), objectStateEntryList.Where(p => p.Entity is BusinessRule).ToList()[0]); } } return(result); }