Exemplo n.º 1
0
        /// <summary>
        /// Create data in database
        /// </summary>
        /// <param name="muscle">Data</param>
        /// <returns>insert data</returns>
        public Muscle Create(Muscle muscle)
        {
            if (muscle == null)
            {
                return(null);
            }

            if (muscle.Id == 0)
            {
                var key = new MuscleKey();
                var sequencerManager = new SequencerManager();
                do
                {
                    key.Id = sequencerManager.GetNextValue(_dbContext, 4, "muscle");
                }while (Get(key) != null); // Test Record exist
                muscle.Id = key.Id;
            }

            if (muscle.Id == 0)
            {
                return(null);
            }

            var row = new MuscleRow();

            MuscleTransformer.ToRow(muscle, row);
            _dbContext.Muscle.Add(row);
            _dbContext.SaveChanges();

            return(MuscleTransformer.ToBean(row));
        }
        /// <summary>
        /// Create data in database
        /// </summary>
        /// <param name="muscle">Data</param>
        /// <returns>insert data</returns>
        public Muscle Create(Muscle muscle)
        {
            if (muscle == null || muscle.Id == 0)
            {
                return(null);
            }

            var row = new MuscleRow();

            MuscleTransformer.ToRow(muscle, row);
            _dbContext.Muscle.Add(row);
            _dbContext.SaveChanges();
            return(MuscleTransformer.ToBean(row));
        }
        /// <summary>
        /// Update data in database
        /// </summary>
        /// <param name="muscle">data</param>
        /// <returns>updated data</returns>
        public Muscle Update(Muscle muscle)
        {
            if (muscle == null || muscle.Id == 0)
            {
                return(null);
            }

            var row = _dbContext.Muscle.Where(m => m.Id == muscle.Id).FirstOrDefault();

            if (row == null)
            {             // No data in database
                return(Create(muscle));
            }
            else
            {             //Modify Data in database
                MuscleTransformer.ToRow(muscle, row);
                _dbContext.SaveChanges();
                return(MuscleTransformer.ToBean(row));
            }
        }
        /// <summary>
        /// Update data in database
        /// </summary>
        /// <param name="muscle">data</param>
        /// <returns>updated data</returns>
        public Muscle Update(Muscle muscle)
        {
            if (muscle == null || muscle.Id == 0)
            {
                return(null);
            }

            var row = _dbContext.Table <MuscleRow>().Where(m => m.Id == muscle.Id).FirstOrDefault();

            if (row == null)
            {             // No data in database
                return(Create(muscle));
            }
            else
            {                           //Modify Data in database
                MuscleTransformer.ToRow(muscle, row);
                _dbContext.Delete(row); //Update don't work... need delete and insert
                _dbContext.Insert(row);
                return(MuscleTransformer.ToBean(row));
            }
        }