Exemple #1
0
        public ActionResult Create([Bind(Include = "Id,Name,Email,Salary,DateBirth,Genre")] PersonPhysicalPersonViewModel person)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    int id = this.GenerateId();

                    context.PERSON.Add(new PERSON()
                    {
                        ID    = id,
                        NAME  = person.Name,
                        EMAIL = person.Email
                    });

                    context.PHYSICALPERSON.Add(new PHYSICALPERSON()
                    {
                        PERSON_ID = id,
                        ID        = id,
                        SALARY    = person.Salary,
                        DATEBIRTH = person.DateBirth,
                        GENRE     = person.Genre
                    });

                    context.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                return(View(person));
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static int Delete_UserSys(int id)
        {
            dbRegistrationContext context1 = new dbRegistrationContext();

            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    USERSYS us = new USERSYS()
                    {
                        ID = id
                    };

                    context1.USERSYS.Attach(us);
                    context1.USERSYS.Remove(us);

                    try
                    {
                        context1.SaveChanges();
                        scope.Complete();
                        return(1);
                    }
                    catch (Exception)
                    {
                        return(-1);
                    }
                }
            }
            catch (Exception)
            {
                return(-1);
            }
        }
        public static int Insert_UserSys(string username, string userpass)
        {
            dbRegistrationContext context1 = new dbRegistrationContext();

            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    int id = 0;

                    try
                    {
                        id = context1.USERSYS.Max(p => p.ID + 1);
                    }
                    catch (Exception)
                    {
                        id = 1;
                    }

                    var sha512 = new SHA512Managed();
                    var bytes  = UTF8Encoding.UTF8.GetBytes(userpass);
                    var hash   = sha512.ComputeHash(bytes);
                    var pass   = Encoding.UTF8.GetString(hash);

                    USERSYS usu = new USERSYS()
                    {
                        ID       = id,
                        USERNAME = username,
                        USERPASS = pass
                    };

                    context1.USERSYS.Add(usu);

                    try
                    {
                        context1.SaveChanges();
                        scope.Complete();
                        return(1);
                    }
                    catch (Exception)
                    {
                        return(-1);
                    }
                }
            }
            catch (Exception)
            {
                return(-1);
            }
        }
        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);
            }
        }
        public static int Delete_PhysicalPerson(int id)
        {
            dbRegistrationContext context1 = new dbRegistrationContext();
            dbRegistrationContext context2 = new dbRegistrationContext();

            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    PERSON pes = new PERSON()
                    {
                        ID = id
                    };


                    PHYSICALPERSON pp = new PHYSICALPERSON()
                    {
                        ID = pes.ID
                    };

                    context2.PHYSICALPERSON.Attach(pp);
                    context2.PHYSICALPERSON.Remove(pp);
                    context1.PERSON.Attach(pes);
                    context1.PERSON.Remove(pes);

                    try
                    {
                        context2.SaveChanges();
                        context1.SaveChanges();
                        scope.Complete();
                        return(1);
                    }
                    catch (Exception)
                    {
                        return(-1);
                    }
                }
            }
            catch (Exception)
            {
                return(-1);
            }
        }
        public static int Insert_PhysicalPerson(string name, string email, decimal salary, DateTime dateBirth, char genre)
        {
            #region With explicit Transaction
            dbRegistrationContext context1 = new dbRegistrationContext();
            dbRegistrationContext context2 = new dbRegistrationContext();

            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    int id = 0;

                    try
                    {
                        id = context1.PERSON.Max(p => p.ID + 1);
                    }
                    catch (Exception)
                    {
                        id = 1;
                    }

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

                    context1.PERSON.Add(pes);

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

                    context2.PHYSICALPERSON.Add(pp);

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

            #region Without explicit Transaction

            /*
             * using (dbRegistrationContext context = new dbRegistrationContext())
             * {
             *  int id = context.PERSON.Max(p => p.ID + 1);
             *
             *  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()
             *  };
             *
             *  context.PERSON.Add(pes);
             *  context.PHYSICALPERSON.Add(pp);
             *  context.SaveChanges();
             *  return 1;
             * }
             */
            #endregion
        }