コード例 #1
0
        public void G_Transactions()
        {
            using (var context = new SamplesContext())
            {
                using (var transaction = context.Database.BeginTransaction())
                {
                    context.Lessons.Find(24).Date = new DateTime(2018, 10, 11);
                    context.SaveChanges();

                    context.Lessons.Find(25).Date = new DateTime(2018, 10, 12);
                    context.SaveChanges();

                    transaction.Commit();
                }
            }
        }
コード例 #2
0
        public void F_RelationsEx()
        {
            using (var context = new SamplesContext())
            {
                var lessons = context.Lessons.Where(x => x.Id > 20);
                var alex    = context.Teachers.Single(x => x.Name == "Aleksandr Kugushev");
                var natalia = context.Teachers.Single(x => x.Name == "Natalia Tcvilikh");

                foreach (var lesson in lessons)
                {
                    lesson.LessonTeachers = lesson.LessonTeachers ?? new List <LessonTeacher>();

                    lesson.LessonTeachers.Add(new LessonTeacher {
                        Teacher = alex
                    });
                    if (lesson.Name == "Cloud")
                    {
                        lesson.LessonTeachers.Add(new LessonTeacher {
                            Teacher = natalia
                        });
                    }
                }
                context.SaveChanges();
            }
        }
コード例 #3
0
 public void F_Relations()
 {
     using (var context = new SamplesContext())
     {
         var lesson = new Lesson
         {
             Id             = 19,
             Date           = new DateTime(2018, 9, 10),
             Name           = "ADO NET",
             LessonTeachers = new List <LessonTeacher>
             {
                 new LessonTeacher
                 {
                     Teacher = new Teacher
                     {
                         Id   = 3,
                         Name = "Igor Pavlovskiy"
                     }
                 }
             }
         };
         context.Add(lesson);
         context.SaveChanges();
     }
 }
コード例 #4
0
 public void D_Update()
 {
     using (var context = new SamplesContext())
     {
         Lesson lesson = context.Lessons.Find(22);
         lesson.Date = DateTime.UtcNow;
         context.SaveChanges();
     }
 }
コード例 #5
0
 public void E_Delete()
 {
     using (var context = new SamplesContext())
     {
         Lesson lesson = context.Lessons.Find(28);
         context.Lessons.Remove(lesson);
         context.SaveChanges();
     }
 }
コード例 #6
0
 public void H_DisconnectedEntites()
 {
     using (var context = new SamplesContext())
     {
         var lesson = new Lesson {
             Id = 22, Date = DateTime.UtcNow, Name = "Entity Framework Core"
         };
         context.Update(lesson);
         context.SaveChanges();
     }
 }
コード例 #7
0
 public void H_DisconnectedEntitesEx()
 {
     using (var context = new SamplesContext())
     {
         var lesson = new Lesson {
             Id = 22, Date = DateTime.UtcNow, Name = "Entity Framework Hard Core"
         };
         var entry = context.Entry(lesson);
         entry.State = EntityState.Modified;
         context.SaveChanges();
     }
 }
コード例 #8
0
        public void M_Tracking()
        {
            using (var context = new SamplesContext())
            {
                var lessons = context.Lessons.AsNoTracking().Where(x => x.Date < DateTime.UtcNow).ToList();
                foreach (var lesson in lessons)
                {
                    lesson.Name = "Hello World";
                }

                context.SaveChanges();
            }
        }
コード例 #9
0
        public void C_Add()
        {
            using (var context = new SamplesContext())
            {
                context.Add(new Lesson {
                    Id = 21, Date = new DateTime(2018, 9, 13), Name = "Net Core"
                });
                context.Add(new Lesson {
                    Id = 22, Date = new DateTime(2018, 9, 17), Name = "Entity Framework"
                });
                context.Add(new Lesson {
                    Id = 23, Date = new DateTime(2018, 9, 20), Name = "Web API"
                });
                context.Add(new Lesson {
                    Id = 24, Date = new DateTime(2018, 9, 24), Name = "Elasticsearch/Redis"
                });
                context.Add(new Lesson {
                    Id = 25, Date = new DateTime(2018, 9, 28), Name = "Docker"
                });
                context.Add(new Lesson {
                    Id = 26, Date = new DateTime(2018, 10, 1), Name = "Cloud"
                });
                context.Add(new Lesson {
                    Id = 27, Date = new DateTime(2018, 10, 4), Name = "Cloud"
                });
                context.Add(new Lesson {
                    Id   = 28, Date = new DateTime(2018, 10, 5),
                    Name = "How to become a millionaire"
                });

                context.Teachers.Add(new Teacher {
                    Name = "Aleksandr Kugushev"
                });
                context.Teachers.Add(new Teacher {
                    Name = "Natalia Tcvilikh"
                });

                context.SaveChanges();
            }
        }