Ejemplo n.º 1
0
        public static void TagEntitiesAsDeleted <T>(this PetShopDBContext dbContext, IList <T> collection)
            where T : class
        {
            if (collection == null || collection.Count <= 0)
            {
                var reason = collection == null
                        ? "null" : "equal or less than zero";
                var message = string.Format("Tagging entities as deleted could " +
                                            "not be executed because to the internal " +
                                            "list being {0}", reason);
                throw new PetsEntityException(message);
            }

            foreach (var item in collection)
            {
                dbContext.Entry <T>(item).State = EntityState.Deleted;

                /* Our design pattern for this application was to
                 * have a sepration were all transactions create
                 * a temporary db-context to communicate with the
                 * database.
                 * Because of the sepertaion, we work in a disconneted
                 * way, which means that we have to manually set
                 * the entity-state of the entity before we save
                 * the object to the database.
                 *
                 * If we do not specify an enity-state, then the
                 * entity framework will throw an error.
                 * */
            }
        }
Ejemplo n.º 2
0
 private void TagEntitiesAsDeleted(PetShopDBContext dbContext)
 {
     foreach (var item in _coursesToDelete)
     {
         dbContext.Entry(item).State = EntityState.Deleted;
     }
 }
Ejemplo n.º 3
0
        private void AddNewMessages(PetShopDBContext dbContext)
        {
            var newMessages = _currentCustomer.Messages
                              .Where(m => m.ID == 0).ToList();

            foreach (var item in newMessages)
            {
                dbContext.Entry(item).State = EntityState.Added;
            }
        }
Ejemplo n.º 4
0
 public void SaveUpdate()
 {
     using (PetShopDBContext _dbcontext = new PetShopDBContext())
     {
         if (_currentStock == null)
         {
             return;
         }
         _dbcontext.Entry(_currentStock).State = EntityState.Modified;
         _dbcontext.SaveChanges();
     }
 }
Ejemplo n.º 5
0
        private void UpdateCourses(PetShopDBContext dbContext)
        {
            var detatchedCollection = this.GetDetatchedCollection <JoinCustomerCourse>(dbContext,
                                                                                       _currentCustomer.Courses, c => c.Courses, true);

            foreach (var item in detatchedCollection)
            {
                var existingData = dbContext.JoinCustomerCourses
                                   .FirstOrDefault(x => x.CUSTOMER_ID == item.CUSTOMER_ID &&
                                                   x.COURSE_ID == item.COURSE_ID);
                dbContext.Entry(item).State =
                    existingData != null ? EntityState.Modified : EntityState.Added;
            }

            _currentCustomer.Courses = detatchedCollection;
        }
Ejemplo n.º 6
0
        private List <TEntity> GetDetatchedCollection <TEntity>(PetShopDBContext dbcontext, ICollection <TEntity> collection,
                                                                Expression <Func <Customer, ICollection <TEntity> > > loadFunction, bool loadPrevious) where TEntity : class
        {
            if (!loadPrevious)
            {
                return(collection.ToList());
            }

            var detatchedCollection = collection.ToList();

            dbcontext.Entry(_currentCustomer)
            .Collection(loadFunction)
            .Load();
            collection.Clear();
            return(detatchedCollection);
        }
        /*public static void LoadAndMarkEnityCollection<S, T>(this IEntityDaBase entityObject, PetShopDBContext dbcontext,
         *  Func<S, ICollection<T>> loadFunction) where S: class where T : class
         * {
         * dbcontext.Entry(entityObject).Collection(loadFunction.ToString()).Load();
         * }*/

        public static void AddNewItems <TEntity>(this PetShopDBContext dbContext, ICollection <TEntity> collection,
                                                 Expression <Func <TEntity, bool> > lamdaQuery) where TEntity : class
        {
            var newItems = collection.AsQueryable().Where(lamdaQuery)
                           .ToList();

            if (newItems != null)
            {
                foreach (var item in newItems)
                {
                    var castedItem = item as IEntityDaBase;
                    if (castedItem != null)
                    {
                        dbContext.Entry(castedItem).State = EntityState.Added;
                    }
                }
            }
        }
Ejemplo n.º 8
0
        public void SaveUpdate()
        {
            try
            {
                if (_currentAccount == null)
                {
                    return;
                }

                using (PetShopDBContext _dbcontext = new PetShopDBContext())
                {
                    _dbcontext.Entry(_currentAccount).State = EntityState.Modified;
                    _dbcontext.SaveChanges();
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show("");
            }
        }
Ejemplo n.º 9
0
        public void SaveUpdate()
        {
            if (_currentAnimal == null)
            {
                var message = "Animal could not be updated because it is null";
                throw new PetsEntityException(message);
            }

            try
            {
                using (PetShopDBContext _context = new PetShopDBContext())
                {
                    _context.Entry(_currentAnimal).State = EntityState.Modified;
                    _context.SaveChanges();
                }
            }
            catch (Exception exception)
            {
                throw;
            }
        }
Ejemplo n.º 10
0
        private void UpdateMessages(PetShopDBContext dbContext)
        {
            var detatchedCollection = this.GetDetatchedCollection <Message>(dbContext,
                                                                            _currentCustomer.Messages, c => c.Messages, false);

            // dbcontext.Entry(_currentCustomer).State = EntityState.Modified;

            /* When tagging child entities in a collection as Entitystate.Modified,
             * make sure you tag the parent entity as Modified, or else it will throw
             * am error.
             * */
            var existingMessages = detatchedCollection.Where(m => m.ID > 0)
                                   .ToList();

            foreach (var item in existingMessages)
            {
                dbContext.Entry(item).State = EntityState.Modified;
            }

            _currentCustomer.Messages = detatchedCollection;
        }
Ejemplo n.º 11
0
        public void SaveUpdate()
        {
            try
            {
                using (PetShopDBContext _dbcontext = new PetShopDBContext())
                {
                    if (_currentCustomer == null)
                    {
                        return;
                    }

                    this.AddNewChildEntities(_dbcontext);
                    _dbcontext.Entry(_currentCustomer).State = EntityState.Modified;
                    this.UpdateExistingChildEntities(_dbcontext);

                    /* Before a parent entity is tagged as medified, make sure all new
                     * child entity objects in its collection are tagged as Entitysate.Added,
                     * if the relationship is a one-to-many.
                     * Because if not, it will throw an error, either saying foreign
                     * key property does not match or something like duplicate id.
                     *
                     * The reason why it throws an error is because, the child entities
                     * at that point in time are tagged as Entitysate.Unchanged.
                     * So when we mark the parent entities as Modified, entity looks
                     * through its collection and will find new entity objects whose
                     * foreign key id has not be assigned.
                     * Or, if the foreign key property is assigned, it will think that
                     * the parent entity is new, which in this case will throw an error
                     * because entity knows there is an enitity object with the same id.
                     * */
                    _dbcontext.SaveChanges();
                }
            }
            catch (Exception exception)
            {
                System.Windows.Forms.MessageBox.Show("Problem encountered during updating customers." +
                                                     "Message" + exception.Message);
            }
        }