Example #1
0
        static async Task Main(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder <SampleContext>();

            optionsBuilder.UseCosmos(
                accountEndpoint: "https://*****:*****@unitest.it");
                marioRossi.Address = new Address("Via Verdi, 24", 20121)
                {
                    City = "Milano"
                };
                marioRossi.ExtraCredits = new System.Collections.Generic.HashSet <ExtraCredit> {
                    new ExtraCredit
                    {
                        Credits  = 10,
                        HoursSum = 150,
                        Name     = "Workshop fisica"
                    },
                    new ExtraCredit
                    {
                        Credits  = 2,
                        HoursSum = 16,
                        Name     = "Workshop quantistica"
                    }
                };

                #endregion

                #region [Student2]
                var giuseppeVerdi = new Student("2020-567")
                {
                    Name    = "Giuseppe",
                    Surname = "Verdi"
                };
                giuseppeVerdi.SetMail("*****@*****.**");
                giuseppeVerdi.Address = new Address("Via Rossi, 44", 20121)
                {
                    City = "Piacenza"
                };
                #endregion

                #region [Student]
                var FrancescoTotti = new Student("2020-323232");
                FrancescoTotti.Name    = "Francesco";
                FrancescoTotti.Surname = "Totti";
                FrancescoTotti.SetMail("*****@*****.**");
                FrancescoTotti.Address = new Address("Via Lupa, 10", 20121)
                {
                    City = "Roma"
                };
                #endregion

                #region [Exam]
                var exam = new Exam("Analisi1-2020-Completo", ExamType.Session, new DateTimeOffset(2020, 06, 01, 09, 00, 00, TimeSpan.Zero));
                exam.Classroom = "Aula 2B";
                exam.AddStudent(marioRossi);
                exam.SetVote(marioRossi, 30, DateTime.Now);
                exam.AddStudent(giuseppeVerdi);
                exam.SetVote(giuseppeVerdi, 18, DateTime.Now);
                exam.AddStudent(FrancescoTotti);
                exam.SetVote(FrancescoTotti, 20, DateTime.Now);
                #endregion

                #region [Course]
                var teacher  = "Isaac Newton";
                var analisi1 = new Course("Analisi 1", teacher, 12);
                analisi1.AddExam(exam);
                #endregion

                db.Courses.Add(analisi1);
                await db.SaveChangesAsync();

                #region [ExamDeleted]
                var examDeleted = new Exam("Fisica1-2020-Completo", ExamType.Session, new DateTimeOffset(2020, 06, 01, 09, 00, 00, TimeSpan.Zero));
                examDeleted.Classroom = "Aula 1B";

                // WARNING
                db.Entry(giuseppeVerdi).State  = EntityState.Detached;
                db.Entry(FrancescoTotti).State = EntityState.Detached;
                // WARNING

                examDeleted.AddStudent(giuseppeVerdi);
                examDeleted.AddStudent(FrancescoTotti);
                #endregion

                #region [CourseToDeleted]
                var teacherDeleted = "Archimede";
                var fisica1        = new Course("Fisica 1", teacherDeleted, 12);
                fisica1.AddExam(examDeleted);
                #endregion

                #region [StudentCollection]
                var marioRossiCollection = new StudentCollection("2020-1234");
                marioRossiCollection.Name    = "Mario";
                marioRossiCollection.Surname = "Rossi";
                marioRossiCollection.SetMail("*****@*****.**");
                marioRossiCollection.Address = new AddressCollection("Via Verdi, 24", 20121)
                {
                    City = "Milano"
                };
                db.Students.Add(marioRossiCollection);
                #endregion



                db.Courses.Add(fisica1);
                await db.SaveChangesAsync();

                // Print
                await PrintCourse(db.Courses);

                var course = await db.Courses.FindAsync(fisica1.Id);

                course.SetExpired();
                await db.SaveChangesAsync();

                await PrintCourse(db.Courses);


                await CosmosClientCourseAsync(db, course);
                await CosmosClientStudentCollectionAsync(db);

                Console.Read();
            }
        }