コード例 #1
0
 public HrUnitOfWork(IContextFactory <DbContext> contextFactory) // , int companyId, string culture
     : base(contextFactory)
 {
     // Initialize
     CompanyRepository          = new CompanyRepository(Context); // , companyId, culture
     PagesRepository            = new PagesRepository(Context);
     MenuRepository             = new MenuRepository(Context);
     PageEditorRepository       = new PageEditorRepository(Context);
     LookUpRepository           = new LookUpRepoitory(Context);
     CompanyStructureRepository = new CompanyStructureRepository(Context);
     JobRepository           = new JobRepository(Context);
     PeopleRepository        = new PeopleRepository(Context);
     PositionRepository      = new PositionRepository(Context);
     BudgetRepository        = new BudgetRepository(Context);
     QualificationRepository = new QualificationRepository(Context);
     LeaveRepository         = new LeaveRepository(Context);
     EmployeeRepository      = new EmployeeRepository(Context);
     CustodyRepository       = new CustodyRepository(Context);
     TrainingRepository      = new TrainingRepository(Context);
     BenefitsRepository      = new BenefitsRepository(Context);
     AudiTrialRepository     = new AudiTrialRepository(Context);
     TerminationRepository   = new TerminationRepository(Context);
     DisciplineRepository    = new DisciplineRepository(Context);
     CheckListRepository     = new CheckListRepository(Context);
     ComplaintRepository     = new ComplaintRepository(Context);
     MessageRepository       = new MessageRepository(Context);
     MedicalRepository       = new MedicalRepository(Context);
     HrLettersRepository     = new HRLettersRepository(Context);
     PayrollRepository       = new PayrollRepository(Context);
     SalaryDesignRepository  = new SalaryDesignRepository(Context);
     NotificationRepository  = new NotificationRepository(Context);
     MissionRepository       = new MissionRepository(Context);
     MeetingRepository       = new MeetingRepository(Context);
 }
コード例 #2
0
        //public class CommandIntercepter : IDbCommandInterceptor
        //{
        //    public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
        //    {
        //    }

        //    public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
        //    {
        //    }

        //    public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
        //    {

        //    }

        //    public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
        //    {
        //    }

        //    public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
        //    {
        //    }

        //    public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
        //    {
        //    }
        //}
        public List <Error> SaveChanges(string culture, out List <DbEntityEntry> entries)
        {
            string message = "";

            entries = null;
            try
            {
                entries = Context.ChangeTracker.Entries().Where(e => e.State == EntityState.Added).ToList();
                var org_entries            = entries.Where(e => e.Entity.ToString() != "Model.Domain.AudiTrail").ToList();
                IList <Original> originals = new List <Original>();

                if (org_entries.Count() > 0)
                {
                    try
                    {
                        // reset for new records
                        foreach (var entry in org_entries)
                        {
                            if (entry.CurrentValues.PropertyNames.Contains("ModifiedUser"))
                            {
                                originals.Add(new Original {
                                    Entity = entry, ModifiedUser = entry.CurrentValues.GetValue <string>("ModifiedUser")
                                });
                                entry.Property("ModifiedUser").CurrentValue = null;
                                entry.Property("ModifiedTime").CurrentValue = null;
                            }
                        }
                    }
                    catch
                    {
                        // do nothing
                    }
                }

                // save all pending changes

                Context.SaveChanges();


                if (originals.Count() > 0) // found new inserted records so resolve references
                {
                    var changed = false;
                    foreach (var org in originals)
                    {
                        if (!org.Entity.CurrentValues.PropertyNames.Contains("Id"))
                        {
                            continue;
                        }

                        int Id = org.Entity.CurrentValues.GetValue <int>("Id");
                        if (Id > 0)
                        {
                            var auditrails = entries.Where(e => e.Entity.ToString() == "Model.Domain.AudiTrail" && e.CurrentValues.GetValue <byte>("Transtype") == 1 && e.CurrentValues.GetValue <string>("ValueBefore") == org.ModifiedUser).Select(e => e.Entity);
                            foreach (var entity in auditrails)
                            {
                                changed = true;
                                var auditrail = (Model.Domain.AudiTrail)entity;
                                auditrail.SourceId    = Id.ToString();
                                auditrail.ValueBefore = null;
                                AudiTrialRepository.Attach(auditrail);
                                AudiTrialRepository.Entry(auditrail).State = EntityState.Modified;
                            }
                        }
                    }

                    if (changed)
                    {
                        Context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                message = HandleDbExceptions(ex, culture);
            }

            if (message.Length > 0)
            {
                return(new List <Error>()
                {
                    new Error {
                        errors = new List <ErrorMessage>()
                        {
                            new ErrorMessage()
                            {
                                message = message
                            }
                        }
                    }
                });
            }
            else
            {
                return(new List <Error>());
            }
        }