コード例 #1
0
        public TransactionList SaveTransactions(TransactionList transactions)
        {
            try
            {
                bool updateFound = false;
                using (EntityContext context = new EntityContext())
                {
                    foreach (var item in transactions)
                    {
                        if (item.TransactionId > 0) //Update
                        {
                            //update database only if the transaction in DB is older
                            var original = context.Transaction
                                                            .Include(x => x.Category)
                                                            .Include(x => x.TransactionType)
                                                            .Include(x => x.TransactionImages)
                                                            .Include(x => x.TransactionReasonType)
                                                            .Where(t => t.TransactionId == item.TransactionId && 
                                                            t.ModifiedDate < item.ModifiedDate &&
                                                            !t.IsDeleted).FirstOrDefault();

                            if (original !=null)
                            {
                                item.HasChanges = false;

                                original.Category = context.Category.Where(k => !k.IsDeleted).Single(p => p.CategoryId == item.Category.CategoryId);
                                //original.Category.CreatedUser = null;
                                //original.Category.ModifiedUser = null;

                                //context.Entry(original).Collection(x => x.TransactionImages).Load();
                                
                                original.TransactionType = context.TypeTransaction.Where(k => !k.IsDeleted).Single(p => p.TypeTransactionId == item.TransactionType.TypeTransactionId);
                                original.TransactionReasonType = context.TransactionReason.Where(k => !k.IsDeleted).Single(p => p.TypeTransactionReasonId == item.TransactionReasonType.TypeTransactionReasonId);

                                original.CreatedUser = context.User.Where(k => !k.IsDeleted).Single(p => p.UserId == item.CreatedUser.UserId);
                                original.ModifiedUser = context.User.Where(k => !k.IsDeleted).Single(p => p.UserId == item.ModifiedUser.UserId);

                                if (item.TransactionImages != null)
                                {
                                    foreach (var x in item.TransactionImages)
                                    {
                                        if (x.TransactionImageId > 0)
                                            original.TransactionImages.Where(k => k.TransactionImageId == x.TransactionImageId).Select(k =>
                                            {
                                                k.Name = x.Name; k.Path = x.Path; k.ModifiedDate = x.ModifiedDate; k.IsDeleted = x.IsDeleted;
                                                k.ModifiedUser = context.User.Where(p => !p.IsDeleted).Single(p => p.UserId == x.ModifiedUser.UserId);
                                                return k;
                                            }).ToList();
                                        else
                                        {
                                            x.CreatedUser = context.User.Where(p => !p.IsDeleted).Single(p => p.UserId == x.CreatedUser.UserId);
                                            x.ModifiedUser = context.User.Where(p => !p.IsDeleted).Single(p => p.UserId == x.ModifiedUser.UserId);

                                            original.TransactionImages.Add(x);
                                        }
                                    }
                                }

                                context.Entry(original).CurrentValues.SetValues(item);
                                updateFound = true;
                                           
                            }
                        }
                        else //Insert
                        {
                            item.Category = context.Category.Where(k => !k.IsDeleted).Single(p => p.CategoryId == item.Category.CategoryId);
                            item.TransactionType = context.TypeTransaction.Where(k => !k.IsDeleted).Single(p => p.TypeTransactionId == item.TransactionType.TypeTransactionId);

                            if (item.TransactionReasonType != null)
                                item.TransactionReasonType = context.TransactionReason.Where(k => !k.IsDeleted).Single(p => p.TypeTransactionReasonId == item.TransactionReasonType.TypeTransactionReasonId);

                            item.CreatedUser = context.User.Where(k => !k.IsDeleted).Single(p => p.UserId == item.CreatedUser.UserId);
                            item.ModifiedUser = context.User.Where(k => !k.IsDeleted).Single(p => p.UserId == item.ModifiedUser.UserId);

                            if (item.TransactionImages != null)
                            {
                                foreach (var x in item.TransactionImages)
                                {
                                    x.CreatedUser = context.User.Where(p => !p.IsDeleted).Single(p => p.UserId == x.CreatedUser.UserId);
                                    x.ModifiedUser = context.User.Where(p => !p.IsDeleted).Single(p => p.UserId == x.ModifiedUser.UserId);

                                    //item.TransactionImages.Add(x);
                                }
                                item.HasPhotos = item.TransactionImages.Count > 0;
                            }

                            
                            context.Transaction.Add(item);
                            updateFound = true;
                        }
                    }

                    if (updateFound)
                        context.SaveChanges();
                }

                transactions.PrepareForServiceSerialization();

                //return GetLatestTransactions();
                return transactions;
            }
            catch (DbEntityValidationException e)
            {
                StringBuilder s = new StringBuilder();
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                        eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage);
                        s.Append(string.Format("{0} - {1}", ve.PropertyName, ve.ErrorMessage));
                    }
                }

                throw new DbEntityValidationException(s.ToString());
            }
            catch (Exception)
            {
                throw;
            }
        }
コード例 #2
0
        public TransactionList GetLatestTransactionsLimit(int latestRecs, int userId)
        {
            try
            {
                TransactionList transList = new TransactionList();
                using (EntityContext context = new EntityContext())
                {
                    context.Configuration.LazyLoadingEnabled = true;

                    var query1 = (from i in context.Transaction
                                .Include(i => i.TransactionType)
                                .Include(i => i.TransactionReasonType)
                                .Include(i => i.Category)
                                .Include(i => i.Category.TypeTransactionReasons)
                                .Include(i => i.ModifiedUser)
                                .Include(i => i.CreatedUser)
                                 where !i.IsDeleted && i.ModifiedUser.UserId == userId
                                 orderby i.TransactionDate descending
                                 select i).Take(latestRecs == 0 ? int.MaxValue : latestRecs).ToList();

                    
                    //investigate if there is a better way to convert the generic list to ObservableCollection
                    foreach (var item in query1)
                    {
                        //item.trans.HasPhotos = item.hasPhotos;
                        item.HasPhotos = context.TransactionImage.Count(x => x.Transaction.TransactionId == item.TransactionId && !x.IsDeleted) > 0;
                        //var trans = item.item;
                        if (item.Category.IsDeleted)
                        {
                            item.Category = context.Category.FirstOrDefault(x => x.Name.Equals("other", StringComparison.InvariantCultureIgnoreCase));
                            item.TransactionReasonType = context.TransactionReason.FirstOrDefault(x => x.Name.Equals("other", StringComparison.InvariantCultureIgnoreCase));
                        }

                        if (item.TransactionReasonType.IsDeleted)
                        {
                            item.TransactionReasonType = context.TransactionReason.FirstOrDefault(x => x.Name.Equals("other", StringComparison.InvariantCultureIgnoreCase));
                        }

                        transList.Add(item);
                    }

                    transList.PrepareForServiceSerialization();

                    return transList;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }