Exemple #1
0
        public ActionResult Edit([Bind(Include = "Id,Name,Email,Salary,DateBirth,Genre")] PersonPhysicalPersonViewModel person)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    PERSON pes = new PERSON()
                    {
                        ID    = person.Id,
                        NAME  = person.Name,
                        EMAIL = person.Email
                    };

                    PHYSICALPERSON pp = new PHYSICALPERSON()
                    {
                        ID        = person.Id,
                        PERSON_ID = person.Id,
                        SALARY    = person.Salary,
                        DATEBIRTH = person.DateBirth,
                        GENRE     = person.Genre
                    };

                    context.PERSON.Add(pes);
                    context.Entry(pes).State = EntityState.Modified;

                    context.PHYSICALPERSON.Add(pp);
                    context.Entry(pp).State = EntityState.Modified;

                    context.SaveChanges();

                    return(RedirectToAction("Index"));
                }
                return(View(person));
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static int Edit_PhysicalPerson(int id, string name, string email, decimal salary, DateTime dateBirth, char genre)
        {
            dbRegistrationContext context1 = new dbRegistrationContext();
            dbRegistrationContext context2 = new dbRegistrationContext();

            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    PERSON pes = new PERSON()
                    {
                        ID    = id,
                        NAME  = name,
                        EMAIL = email
                    };

                    PHYSICALPERSON pp = new PHYSICALPERSON()
                    {
                        ID        = pes.ID,
                        PERSON_ID = pes.ID,
                        SALARY    = salary,
                        DATEBIRTH = dateBirth,
                        GENRE     = genre.ToString()
                    };

                    context1.PERSON.Attach(pes);
                    context1.Entry(pes).State = EntityState.Modified;
                    context2.PHYSICALPERSON.Attach(pp);
                    context2.Entry(pp).State = EntityState.Modified;

                    try
                    {
                        context2.SaveChanges();
                        context1.SaveChanges();
                        scope.Complete();
                        return(1);
                    }
                    catch (Exception)
                    {
                        return(-1);
                    }
                }
            }
            catch (Exception)
            {
                return(-1);
            }
        }