Ejemplo n.º 1
0
        protected virtual void Save(DbSet <TEntity> set, TEntity entity)
        {
            var entry = _context.Entry(entity);

            if (entry == null || entry.State == EntityState.Detached)
            {
                set.Add(entity);
            }
        }
        public async Task <IActionResult> PutPerson(int id, PersonDTO personDTO)
        {
            if (id != personDTO.Id)
            {
                return(BadRequest());
            }

            var person = await _context.People
                         .Include(p => p.Address)
                         .Include(p => p.Interests)
                         .FirstOrDefaultAsync(p => p.Id == id);

            person.FirstName       = personDTO.FirstName;
            person.MiddleName      = personDTO.MiddleName;
            person.LastName        = personDTO.LastName;
            person.Address         = personDTO.Address;
            person.PersonAddressId = personDTO.PersonAddressId;
            person.DOB             = new DateTime(personDTO.DOB.Year, personDTO.DOB.Month, personDTO.DOB.Day);
            person.PathToAvatar    = personDTO.PathToAvatar;
            person.IsFavorite      = personDTO.IsFavorite;

            _context.Entry(person).State = EntityState.Modified;
            if (personDTO.Address != null)
            {
                _context.Entry(personDTO.Address).State = EntityState.Modified;
            }
            if (personDTO.Interests != null)
            {
                foreach (var interest in personDTO.Interests)
                {
                    _context.Entry(interest).State = EntityState.Modified;
                }
            }

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PersonExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 3
0
        public async Task <IHttpActionResult> PutPerson(int id, Person person)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != person.Id)
            {
                return(BadRequest());
            }

            db.Entry(person).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PersonExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public async Task <IActionResult> PutPerson(int id, Person person)
        {
            if (id != person.Id)
            {
                return(BadRequest());
            }

            _context.Entry(person).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PersonExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutPerson(int id, Person person)
        {
            if (id != person.Id)
            {
                return(BadRequest());
            }

            person.IsActive = true;
            var local = _context.Set <Person>()
                        .Local
                        .FirstOrDefault(entry => entry.Id.Equals(id));

            if (local != null)
            {
                // detach
                _context.Entry(local).State = EntityState.Detached;
            }
            _context.Entry(person).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PersonExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        private async void btn_delete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are Yor Sure to Delete this Record?", "EF Crud Operation,", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                using (PersonDbContext db = new PersonDbContext())
                {
                    var entry = db.Entry(people);
                    if (entry.State == EntityState.Detached)
                    {
                        db.People.Attach(people);
                    }
                    db.People.Remove(people);
                    await db.SaveChangesAsync();

                    DataLoad();
                    Clear();
                    MessageBox.Show("Deleted Successfully");
                }
            }
        }
Ejemplo n.º 7
0
        public async Task UpdateItem(Person item)
        {
            try
            {
                var personToUpdate = _personDbContext.People
                                     .Where(p => p.PersonId == item.PersonId).FirstOrDefault();

                if (personToUpdate != null)
                {
                    _personDbContext.Entry(personToUpdate).CurrentValues.SetValues(item);
                }

                await _personDbContext.SaveChangesAsync();

                _logger.LogInformation($"Updated details for item with id {item.PersonId}");
            }
            catch (Exception ex)
            {
                _logger.LogError($"An error occured while updating item with Id {item.PersonId}. Exception details :{ex.Message}");
            }
        }
        private async void btn_save_Click(object sender, EventArgs e)
        {
            people.Name     = txbx_name.Text.Trim();
            people.Surname  = txbx_surname.Text.Trim();
            people.Email    = txbx_email.Text.Trim();
            people.Password = txbx_password.Text.Trim();

            List <ValidationResult> validationResults = new List <ValidationResult>();

            ValidationContext validationContext = new ValidationContext(people);

            if (!Validator.TryValidateObject(people, validationContext, validationResults, true))
            {
                lbl_errors.Text = string.Empty;
                foreach (ValidationResult item in validationResults)
                {
                    lbl_errors.Text += item.ErrorMessage + "\n";
                }
            }
            else
            {
                using (PersonDbContext db = new PersonDbContext())
                {
                    if (people.Id == 0) //insert
                    {
                        db.People.Add(people);
                    }
                    else //update
                    {
                        db.Entry(people).State = EntityState.Modified;
                    }
                    await db.SaveChangesAsync();
                }
                Clear();
                DataLoad();
                MessageBox.Show("Submitted Successfully");
            }
        }
Ejemplo n.º 9
0
 public ActionResult Edit(Person person, int[] selectedSkills)
 {
     if (ModelState.IsValid)
     {
         Person newPerson = db.People.Find(person.Id);
         newPerson.Name        = person.Name;
         newPerson.SecondName  = person.SecondName;
         newPerson.PhoneNumber = person.PhoneNumber;
         newPerson.CompanyId   = person.CompanyId == 0?null: person.CompanyId;
         newPerson.Skills.Clear();
         if (selectedSkills != null)
         {
             foreach (var skill in db.Skills.Where(skill => selectedSkills.Contains(skill.Id)))
             {
                 newPerson.Skills.Add(skill);
             }
         }
         db.Entry(newPerson).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(person));
 }
        public async Task <IActionResult> PutPersonInterest(int id, PersonInterest personInterest)
        {
            personInterest.Id = id;

            _context.Entry(personInterest).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PersonInterestExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 11
0
 public void Update(T o)
 {
     dbContext.Set <T>().Attach(o);
     dbContext.Entry(o).State = EntityState.Modified;
     dbContext.SaveChanges();
 }