Пример #1
0
        public static void GuardarStudentDB()
        {
            //Ejemplo de guardar en DB
            Contexto context = new Contexto();

            try
            {
                var auxStudent = new Studen()
                {
                    StudentId = 0,
                    FirstName = "Emminton Manuel",
                    LastName  = "Ueña Santana"
                };
                context.Students.Add(auxStudent);
                bool save = context.SaveChanges() > 0;

                if (save)
                {
                    Console.WriteLine("The Student was sucessfully saved!!");
                }
                else
                {
                    Console.WriteLine("We cant save the student..");
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                context.Dispose();
            }
        }
Пример #2
0
        public async Task <int> create(Studen studen)
        {
            studen.CreatedDate = DateTime.Now;
            studen.CreatedBy   = "System";
            _dbContext.Studens.Add(studen);
            await _dbContext.SaveChangesAsync();

            return(1);
        }
Пример #3
0
        public async Task <int> update(int id, Studen studen)
        {
            if (id != studen.ID)
            {
                return(0);
            }
            var modal = _dbContext.Studens.Find(studen.ID);

            modal.Name        = studen.Name;
            modal.Address     = studen.Address;
            modal.Email       = studen.Email;
            modal.UpdatedDate = DateTime.Now;
            modal.UpdatedBy   = "System";

            _dbContext.Entry(modal).State = EntityState.Modified;
            await _dbContext.SaveChangesAsync();

            return(1);
        }
Пример #4
0
        public static void UpdatingOnDisconnectedScenario()
        {
            Contexto context = new Contexto();

            try
            {
                var modifiedStudent1 = new Studen()
                {
                    StudentId = 1,
                    FirstName = "jerson",
                    LastName  = "gutierrez"
                };

                var modifiedStudent2 = new Studen()
                {
                    StudentId = 2,
                    FirstName = "Steve",
                    LastName  = "mendez"
                };

                List <Studen> modifiedStudents = new List <Studen>()
                {
                    modifiedStudent1,
                    modifiedStudent2,
                };

                context.UpdateRange(modifiedStudents);
                bool modified = context.SaveChanges() > 0;
                if (modified)
                {
                    Console.WriteLine("Modified");
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                context.Dispose();
            }
        }