public void StackOverflowTest()
        {
            using (var db = new UnitTestContext())
            {
                var i1 = new Instructor
                {
                    FirstName        = "Mickey",
                    LastName         = "Mouse",
                    HireDate         = new DateTime(1928, 5, 15),
                    OfficeAssignment = new OfficeAssignment {
                        Location = "Room 1A"
                    }
                };
                db.Instructors.Add(i1);
                db.SaveChanges();

                var d1 = new Department
                {
                    Name   = "Computer Science",
                    Budget = 10000000,
                };

                var c1 = new Course
                {
                    Credits    = 4,
                    Title      = "Foundations of Data Science",
                    Department = d1
                };

                db.Courses.Add(c1);
                db.SaveChanges();

                i1.Courses.Add(c1);
                db.SaveChanges();

                Assert.AreEqual(1, db.Instructors.SelectMany(i => i.Courses).Distinct().Count());

                var instructor = db.Instructors
                                 .Include(i => i.Courses.Select(c => c.Department))
                                 .Include(i => i.OfficeAssignment)
                                 .Single();

                instructor.InstructorID = 0;
                instructor.OfficeAssignment.InstructorID = 0;

                var request = new BulkInsertRequest <Instructor>
                {
                    Entities = new[] { instructor }.ToList(),
                    EnableRecursiveInsert      = EnableRecursiveInsert.Yes,
                    AllowNotNullSelfReferences = AllowNotNullSelfReferences.No
                };
                db.BulkInsertAll(request);


                Assert.AreEqual(2, db.Instructors.SelectMany(i => i.Courses).Count());
            }
        }
        public void DeleteNotExistingEntities2()
        {
            var p0 = new Person
            {
                FirstName = "Angelica",
                LastName  = "Tånneryd",
                BirthDate = DateTime.Now
            };
            var p1 = new Person
            {
                FirstName      = "Arvid",
                LastName       = "Tånneryd",
                BirthDate      = DateTime.Now,
                EmployeeNumber = 0,
                Mother         = p0
            };
            var p2 = new Person
            {
                FirstName      = "Viktor",
                LastName       = "Tånneryd",
                BirthDate      = DateTime.Now,
                EmployeeNumber = 0,
                Mother         = p0
            };

            using (var db = new UnitTestContext())
            {
                db.People.AddRange(new[] { p0, p1, p2 });
                db.SaveChanges();

                var people = db.People.OrderBy(p => p.FirstName).ToArray();
                Assert.AreEqual(3, people.Length);
                Assert.AreEqual(p0.Id, people[0].Id);
                Assert.AreEqual(p1.Id, people[1].Id);
                Assert.AreEqual(p2.Id, people[2].Id);

                // Delete all children of p0 in the database
                // that are not in the Items list. Make sure
                // that we can differentiate between null and
                // column default values.
                // Setting the EmployeeNumber values to null
                // should make those two objects NOT match
                // with the records in the database and thus
                // only the mother record should be left after
                // this operation.
                p1.EmployeeNumber = null;
                p2.EmployeeNumber = null;
                db.BulkDeleteNotExisting <Person, Person>(new BulkDeleteRequest <Person>(
                                                              new[] { new SqlCondition("MotherId", p0.Id) },
                                                              new[] { "FirstName", "EmployeeNumber", "LastName" })
                {
                    Items = new[] { p1, p2 }.ToList()
                });

                people = db.People.OrderBy(p => p.FirstName).ToArray();
                Assert.AreEqual(1, people.Length);
                Assert.AreEqual(p0.Id, people[0].Id);
            }
        }
Ejemplo n.º 3
0
        public void ZeroShouldNotMatchNullWhenSelectExisting()
        {
            using (var db = new UnitTestContext())
            {
                db.Prices.Add(new Price()
                {
                    Date = new DateTime(2019, 1, 1), Name = "ERICB", Value = 80
                });
                db.Prices.Add(new Price()
                {
                    Date = new DateTime(2019, 1, 2), Name = "ERICB", Value = 81
                });
                db.Prices.Add(new Price()
                {
                    Date = new DateTime(2019, 1, 3), Name = "ERICB", Value = 82
                });
                db.Prices.Add(new Price()
                {
                    Date = new DateTime(2019, 1, 4), Name = "ERICB", Value = 0
                });
                db.Prices.Add(new Price()
                {
                    Date = new DateTime(2019, 1, 5), Name = "ERICB", Value = 86
                });
                db.SaveChanges();

                var prices = new[]
                {
                    new Price()
                    {
                        Date = new DateTime(2019, 1, 1), Name = "ERICB", Value = 80
                    },
                    new Price()
                    {
                        Date = new DateTime(2019, 1, 2), Name = "ERICB", Value = 81
                    },
                    new Price()
                    {
                        Date = new DateTime(2019, 1, 3), Name = "ERICB", Value = 82
                    },
                    new Price()
                    {
                        Date = new DateTime(2019, 1, 4), Name = "ERICB", Value = null
                    },
                    new Price()
                    {
                        Date = new DateTime(2019, 1, 5), Name = "ERICB", Value = 86
                    }
                };

                var existing = db.BulkSelectExisting <Price, Price>(
                    new BulkSelectRequest <Price>(new[] { "Date", "Name", "Value" }, prices));
                Assert.AreEqual(4, existing.Count);
                Assert.AreSame(prices[0], existing[0]);
                Assert.AreSame(prices[1], existing[1]);
                Assert.AreSame(prices[2], existing[2]);
                Assert.AreSame(prices[4], existing[3]);
            }
        }
        public void InsertEntityWithComputedColumnInTableWithUserGeneratedPrimaryKeyShouldWork()
        {
            using (var db = new UnitTestContext())
            {
                for (int i = 0; i < 10; i++)
                {
                    db.Journals.Add(new Journal()
                    {
                        Id = Guid.NewGuid()
                    });
                }
                db.SaveChanges();

                var batchInvoice = new BatchInvoice {
                    Id = Guid.NewGuid()
                };

                foreach (var journal in db.Journals.ToList())
                {
                    var invoice = new Invoice()
                    {
                        Id = Guid.NewGuid(), Gross = 10, Net = 3
                    };
                    invoice.Journals.Add(new InvoiceItem()
                    {
                        JournalId = journal.Id
                    });
                    batchInvoice.Invoices.Add(new BatchInvoiceItem()
                    {
                        Id = Guid.NewGuid(), Invoice = invoice
                    });
                }

                var req = new BulkInsertRequest <BatchInvoice>
                {
                    Entities = new[] { batchInvoice }.ToList(),
                    AllowNotNullSelfReferences = AllowNotNullSelfReferences.No,
                    SortUsingClusteredIndex    = true,
                    EnableRecursiveInsert      = EnableRecursiveInsert.Yes
                };
                var response = db.BulkInsertAll(req);

                Assert.AreEqual(1, db.BatchInvoices.ToArray().Count());
                Assert.AreEqual(10, db.BatchInvoiceItems.ToArray().Count());
                Assert.AreEqual(10, db.Invoices.ToArray().Count());
                Assert.AreEqual(10, db.InvoiceItems.ToArray().Count());
            }
        }
        public void DeleteNotExistingEntities()
        {
            var p0 = new Person
            {
                FirstName = "Angelica",
                LastName  = "Tånneryd",
                BirthDate = DateTime.Now
            };
            var p1 = new Person
            {
                FirstName = "Arvid",
                LastName  = "Tånneryd",
                BirthDate = DateTime.Now,
                Mother    = p0
            };
            var p2 = new Person
            {
                FirstName = "Inga-Lill",
                LastName  = "Tånneryd",
                BirthDate = DateTime.Now
            };
            var p3 = new Person
            {
                FirstName = "Linus",
                LastName  = "Tånneryd",
                BirthDate = DateTime.Now,
                Mother    = p0
            };
            var p4 = new Person
            {
                FirstName = "Måns",
                LastName  = "Tånneryd",
                BirthDate = DateTime.Now,
                Mother    = p2
            };
            var p5 = new Person
            {
                FirstName = "Viktor",
                LastName  = "Tånneryd",
                BirthDate = DateTime.Now,
                Mother    = p0
            };

            using (var db = new UnitTestContext())
            {
                db.People.AddRange(new[] { p0, p1, p2, p3, p4, p5 });
                db.SaveChanges();

                var people = db.People.OrderBy(p => p.FirstName).ToArray();
                Assert.AreEqual(6, people.Length);
                Assert.AreEqual(p0.Id, people[0].Id);
                Assert.AreEqual(p1.Id, people[1].Id);
                Assert.AreEqual(p2.Id, people[2].Id);
                Assert.AreEqual(p3.Id, people[3].Id);
                Assert.AreEqual(p4.Id, people[4].Id);
                Assert.AreEqual(p5.Id, people[5].Id);

                // Delete all children of p0 in the database
                // that are not in the Items list.
                db.BulkDeleteNotExisting <Person, Person>(new BulkDeleteRequest <Person>(
                                                              new[] { new SqlCondition("MotherId", p0.Id) },
                                                              new[] { "FirstName", "LastName" })
                {
                    Items = new[] { p1, p3 }.ToList()
                });

                people = db.People.OrderBy(p => p.FirstName).ToArray();
                Assert.AreEqual(5, people.Length);
                Assert.AreEqual(p0.Id, people[0].Id);
                Assert.AreEqual(p1.Id, people[1].Id);
                Assert.AreEqual(p2.Id, people[2].Id);
                Assert.AreEqual(p3.Id, people[3].Id);
                Assert.AreEqual(p4.Id, people[4].Id);
            }
        }
        protected void CleanupUnitTestContext()
        {
            var db = new UnitTestContext();

            db.BatchInvoiceItems.RemoveRange(db.BatchInvoiceItems.ToArray());
            db.InvoiceItems.RemoveRange(db.InvoiceItems.ToArray());
            db.Invoices.RemoveRange(db.Invoices.ToArray());
            db.Journals.RemoveRange(db.Journals.ToArray());
            db.BatchInvoices.RemoveRange(db.BatchInvoices.ToArray());
            db.SaveChanges();

            db.Database.ExecuteSqlRaw(@"DELETE FROM [dbo].[PlayerWithDbGeneratedGuid]");
            db.Database.ExecuteSqlRaw(@"DELETE FROM [dbo].[CoachWithDbGeneratedGuid]");
            db.Database.ExecuteSqlRaw(@"DELETE FROM [dbo].[TeamWithDbGeneratedGuid]");
            db.Database.ExecuteSqlRaw(@"DELETE FROM [dbo].[CoachTeamsWithDbGeneratedGuid]");

            db.Database.ExecuteSqlRaw(@"DELETE FROM [dbo].[PlayerWithUserGeneratedGuid]");
            db.Database.ExecuteSqlRaw(@"DELETE FROM [dbo].[CoachWithUserGeneratedGuid]");
            db.Database.ExecuteSqlRaw(@"DELETE FROM [dbo].[TeamWithUserGeneratedGuid]");
            db.Database.ExecuteSqlRaw(@"DELETE FROM [dbo].[CoachTeamsWithUserGeneratedGuid]");

            db.OfficeAssignments.RemoveRange(db.OfficeAssignments.ToArray());
            foreach (var i in db.Instructors)
            {
                i.CourseInstructors.Clear();
            }
            foreach (var i in db.Courses)
            {
                i.CourseInstructors.Clear();
            }
            db.CourseInstructors.RemoveRange(db.CourseInstructors.ToArray());
            db.Instructors.RemoveRange(db.Instructors.ToArray());
            db.Courses.RemoveRange(db.Courses.ToArray());
            db.Departments.RemoveRange(db.Departments.ToArray());
            db.SaveChanges();

            db.ReservedSqlKeywords.RemoveRange(db.ReservedSqlKeywords.ToArray());
            db.Coordinates.RemoveRange(db.Coordinates.ToArray());
            db.Points.RemoveRange(db.Points.ToArray());
            db.SaveChanges();

            db.Blogs.RemoveRange(db.Blogs.ToArray());
            db.Posts.RemoveRange(db.Posts.ToArray());
            db.Keywords.RemoveRange(db.Keywords.ToArray());
            db.Visitors.RemoveRange(db.Visitors.ToArray());
            db.SaveChanges();

            db.People.RemoveRange(db.People.ToArray());
            db.SaveChanges();

            db.Employees.RemoveRange(db.Employees.ToArray());
            foreach (var c in db.Companies)
            {
                c.ParentCompanyId = 0;
                c.ParentCompany   = null;
            }
            db.Companies.RemoveRange(db.Companies.ToArray());
            db.SaveChanges();

            db.Composites.RemoveRange(db.Composites.ToArray());
            db.Primes.RemoveRange(db.Primes.ToArray());
            db.Numbers.RemoveRange(db.Numbers.ToArray());
            db.Parities.RemoveRange(db.Parities.ToArray());
            db.SaveChanges();

            db.Prices.RemoveRange(db.Prices.ToArray());
            db.SaveChanges();

            db.LogItems.RemoveRange(db.LogItems.ToArray());
            db.SaveChanges();
        }
Ejemplo n.º 7
0
 public void Delete(TEntity T)
 {
     _dbSet.Remove(T);
     _context.SaveChanges();
 }