Esempio n. 1
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 SelectExistingFromTableWithUserGeneratedGuidAsPrimaryKey()
        {
            using (var db = new UnitTestContext())
            {
                var teams = new List <TeamWithUserGeneratedGuidKey>();

                // Add ten teams to the database (Team 0 - Team 9)
                for (int i = 0; i < 10; i++)
                {
                    teams.Add(new TeamWithUserGeneratedGuidKey()
                    {
                        Id = Guid.NewGuid(), Name = $"Team #{i}"
                    });
                }

                // Save the ten first teams to the database.
                db.BulkInsertAll(new BulkInsertRequest <TeamWithUserGeneratedGuidKey>
                {
                    Entities = teams,
                });

                // Add another ten teams (Team 10 - Team 19) to
                // the list but not to the database.
                for (int i = 10; i < 20; i++)
                {
                    teams.Add(new TeamWithUserGeneratedGuidKey()
                    {
                        Id = Guid.NewGuid(), Name = $"Team #{i}"
                    });
                }

                // The only teams we should get back out of the 20 teams (Team 0 - Team 19)
                // are the first ten that we saved to the database.
                var existingTeams = db.BulkSelectExisting <TeamWithUserGeneratedGuidKey, TeamWithUserGeneratedGuidKey>(new BulkSelectRequest <TeamWithUserGeneratedGuidKey>(new[] { "Id" }, teams));
                existingTeams = existingTeams.OrderBy(t => t.Name).ToList();
                Assert.AreEqual(10, existingTeams.Count);
                for (int i = 0; i < 10; i++)
                {
                    Assert.AreEqual(teams[i].Id, existingTeams[i].Id);
                    Assert.AreEqual(teams[i].Name, existingTeams[i].Name);
                }
            }
        }
        public void EntitiesOfDifferentTypeMatchingExistingEntitiesShouldBeSelected()
        {
            using (var db = new UnitTestContext())
            {
                var now = DateTime.Now;

                // Save 200 numbers (1 to 200) to the database.
                var numbers = GenerateNumbers(1, 200, now).ToArray();
                db.BulkInsertAll(new BulkInsertRequest <Number>
                {
                    Entities = numbers,
                    EnableRecursiveInsert = EnableRecursiveInsert.Yes
                });

                // Create a list of 100 numbers with values 151 to 250
                var nums = GenerateNumbers(151, 100, now)
                           .Select(n => new Num {
                    Val = n.Value
                })
                           .ToList();

                // Numbers 151 to 200 out of 151 to 250 should be selected.
                var existingNumbers = db.BulkSelectExisting <Num, Number>(new BulkSelectRequest <Num>
                {
                    Items = nums,
                    KeyPropertyMappings = new[]
                    {
                        new KeyPropertyMapping
                        {
                            ItemPropertyName   = "Val",
                            EntityPropertyName = "Value"
                        },
                    }
                }).ToArray();

                Assert.AreEqual(50, existingNumbers.Length);
                for (int i = 0; i < 50; i++)
                {
                    Assert.AreEqual(nums[i], existingNumbers[i]);
                }
            }
        }