コード例 #1
0
 public virtual List <TEntity> AddUpdate(List <TEntity> entities, AddUpdateOptions options = null)
 {
     entities.ForEach((e) =>
     {
         this.ResolveDuplicateRefIssue(e);
     });
     return(Repository.AddUpdate(entities, options));
 }
コード例 #2
0
ファイル: BookingManager.cs プロジェクト: ajarwan/Skillaround
        public override Booking AddUpdate(Booking entity, AddUpdateOptions options = null)
        {
            if (entity.State == BaseState.Added)
            {
                entity = base.AddUpdate(entity, options);
                Unit.SaveChanges();

                Random rnd = new Random();
                entity.BookingNumber = entity.Id.ToString().PadLeft(2, '0') + rnd.Next(100, 999);
                entity.State         = BaseState.Modified;

                entity = base.AddUpdate(entity, options);
                Unit.SaveChanges();

                this.SendBookingNotificationEmail(entity);
                return(entity);
            }
            else
            {
                return(base.AddUpdate(entity, options));
            }
        }
コード例 #3
0
        public TEntity AddOrUpdateByKey <TEntity>(TEntity entity, bool addWithoutRelatedEntities = false,
                                                  Action <TEntity> updatePropertiesAction        = null,
                                                  AddUpdateOptions addUpdateOptions = AddUpdateOptions.Add | AddUpdateOptions.Update, bool saveChanges = true)
            where TEntity : class, IEntity, new()
        {
            if (entity.Key == Guid.Empty)
            {
                return(null);
            }

            Action <TEntity> updateProperties = null;

            if (updatePropertiesAction != null)
            {
                updateProperties = updatePropertiesAction;
            }

            var updated = AddOrUpdate(entity, existing => existing.Key == entity.Key, addWithoutRelatedEntities,
                                      updateProperties, addUpdateOptions, saveChanges);

            return(updated);
        }
コード例 #4
0
        public override User AddUpdate(User entity, AddUpdateOptions options = null)
        {
            if (entity.State == BaseState.Added)
            {
                //Send Welcome Email
                entity.UserUniqueId = new Guid();

                entity = base.AddUpdate(entity, options);
                entity.UserUniqueId = Guid.NewGuid();
                Unit.SaveChanges();
                this.SendWelcomeMessage(entity);

                return(entity);
            }
            else if (entity.State == BaseState.Modified)
            {
                entity.PasswordHash = this.FindById(entity.Id).PasswordHash;
                return(base.AddUpdate(entity, options));
            }
            else
            {
                return(base.AddUpdate(entity, options));
            }
        }
コード例 #5
0
 public int DeleteBatch(Expression <Func <TEntity, bool> > filter, AddUpdateOptions options = null)
 {
     return(Repository.DeleteBatch(filter, options));
 }
コード例 #6
0
 public int UpdateBatch(Expression <Func <TEntity, bool> > filter, Dictionary <string, string> updateProps, AddUpdateOptions options = null)
 {
     return(Repository.UpdateBatch(filter, updateProps, options));
 }
コード例 #7
0
 public virtual TEntity AddUpdate(TEntity entity, AddUpdateOptions options = null)
 {
     this.ResolveDuplicateRefIssue(entity);
     return(Repository.AddUpdate(entity, options));
 }
コード例 #8
0
        public TWithRelationalId AddOrUpdate <TWithRelationalId>(TWithRelationalId withRelationalId,
                                                                 Expression <Func <TWithRelationalId, bool> > queryExpression, bool addWithoutRelatedEntities = false,
                                                                 Action <TWithRelationalId> updatePropertiesAction = null,
                                                                 AddUpdateOptions addUpdateOptions = AddUpdateOptions.Add | AddUpdateOptions.Update, bool saveChanges = true)
            where TWithRelationalId : class, IWithRelationalId, new()
        {
            TWithRelationalId addedOrUpdatedItem = null;

            var dbSet         = DbContext.Set <TWithRelationalId>();
            var existingItems = dbSet.Where(queryExpression).ToList();

            if (!existingItems.Any())
            {
                if (!addUpdateOptions.HasFlag(AddUpdateOptions.Add))
                {
                    return(null);
                }

                if (addWithoutRelatedEntities)
                {
                    dbSet.Attach(withRelationalId);
                    DbContext.Entry(withRelationalId).State = EntityState.Added;
                }
                else
                {
                    dbSet.Add(withRelationalId);
                }
                addedOrUpdatedItem = withRelationalId;
            }
            else if (updatePropertiesAction == null)
            {
                var firstMatch = existingItems.First();
                withRelationalId.SetId(firstMatch.Id);

                if (firstMatch.TimeCreated.HasValue)
                {
                    withRelationalId.SetTimeCreated(firstMatch.TimeCreated);
                }

                DbContext.Entry(firstMatch).State = EntityState.Detached;

                dbSet.Attach(withRelationalId);
                DbContext.Entry(withRelationalId).State = EntityState.Modified;
                addedOrUpdatedItem = withRelationalId;
            }
            else
            {
                foreach (var item in existingItems)
                {
                    updatePropertiesAction(item);
                    DbContext.Entry(item).State = EntityState.Modified;
                    addedOrUpdatedItem          = addedOrUpdatedItem ?? item;
                }
            }

            if (saveChanges)
            {
                DbContext.SaveChanges();
            }

            return(addedOrUpdatedItem);
        }