예제 #1
0
        private void RunInsertOrUpdateOrDelete(bool isBulk)
        {
            using (var context = new TestContext(ContextUtil.GetOptions()))
            {
                var entities    = new List <Item>();
                var dateTimeNow = DateTime.Now;
                for (int i = 2; i <= EntitiesNumber; i += 2)
                {
                    entities.Add(new Item
                    {
                        ItemId      = i,
                        Name        = "name InsertOrUpdateOrDelete " + i,
                        Description = "info",
                        Quantity    = i,
                        Price       = i / (i % 5 + 1),
                        TimeUpdated = dateTimeNow
                    });
                }
                if (isBulk)
                {
                    var bulkConfig = new BulkConfig()
                    {
                        SetOutputIdentity = true, CalculateStats = true
                    };
                    context.BulkInsertOrUpdateOrDelete(entities, bulkConfig, (a) => WriteProgress(a));
                    Assert.Equal(0, bulkConfig.StatsInfo.StatsNumberInserted);
                    Assert.Equal(EntitiesNumber / 2, bulkConfig.StatsInfo.StatsNumberUpdated);
                    Assert.Equal(EntitiesNumber / 2, bulkConfig.StatsInfo.StatsNumberDeleted);
                }
                else
                {
                    var existingItems = context.Items;
                    var removedItems  = existingItems.Where(x => !entities.Any(y => y.ItemId == x.ItemId));
                    context.Items.RemoveRange(removedItems);
                    context.Items.AddRange(entities);
                    context.SaveChanges();
                }
            }
            using (var context = new TestContext(ContextUtil.GetOptions()))
            {
                //int entitiesCount = ItemsCountQuery(context);
                int entitiesCount = context.Items.Count();
                //Item lastEntity = LastItemQuery(context);
                Item firstEntity = context.Items.OrderBy(a => a.ItemId).FirstOrDefault();
                Item lastEntity  = context.Items.OrderByDescending(a => a.ItemId).FirstOrDefault();

                Assert.Equal(EntitiesNumber / 2, entitiesCount);
                Assert.NotNull(firstEntity);
                Assert.Equal("name InsertOrUpdateOrDelete 2", firstEntity.Name);
                Assert.NotNull(lastEntity);
                Assert.Equal("name InsertOrUpdateOrDelete " + EntitiesNumber, lastEntity.Name);
            }
        }
예제 #2
0
        private void RunInsertOrUpdateOrDelete(bool isBulk)
        {
            using var context = new TestContext(ContextUtil.GetOptions());

            var entities    = new List <Item>();
            var dateTimeNow = DateTime.Now;

            for (int i = 2; i <= EntitiesNumber; i += 2)
            {
                entities.Add(new Item
                {
                    ItemId      = i,
                    Name        = "name InsertOrUpdateOrDelete " + i,
                    Description = "info",
                    Quantity    = i,
                    Price       = i / (i % 5 + 1),
                    TimeUpdated = dateTimeNow
                });
            }

            int?keepEntityItemId = null;

            if (isBulk)
            {
                var bulkConfig = new BulkConfig()
                {
                    SetOutputIdentity = true, CalculateStats = true
                };

                keepEntityItemId = 3;
                bulkConfig.SetSynchronizeFilter <Item>(e => e.ItemId != keepEntityItemId.Value);
                context.BulkInsertOrUpdateOrDelete(entities, bulkConfig, (a) => WriteProgress(a));
                Assert.Equal(0, bulkConfig.StatsInfo.StatsNumberInserted);
                Assert.Equal(EntitiesNumber / 2, bulkConfig.StatsInfo.StatsNumberUpdated);
                Assert.Equal((EntitiesNumber / 2) - 1, bulkConfig.StatsInfo.StatsNumberDeleted);
            }
            else
            {
                var existingItems = context.Items;
                var removedItems  = existingItems.Where(x => !entities.Any(y => y.ItemId == x.ItemId));
                context.Items.RemoveRange(removedItems);
                context.Items.AddRange(entities);
                context.SaveChanges();
            }

            // TEST
            int  entitiesCount = context.Items.Count();
            Item firstEntity   = context.Items.OrderBy(a => a.ItemId).FirstOrDefault();
            Item lastEntity    = context.Items.OrderByDescending(a => a.ItemId).FirstOrDefault();

            Assert.Equal((EntitiesNumber / 2) + (keepEntityItemId != null ? 1 : 0), entitiesCount);
            Assert.NotNull(firstEntity);
            Assert.Equal("name InsertOrUpdateOrDelete 2", firstEntity.Name);
            Assert.NotNull(lastEntity);
            Assert.Equal("name InsertOrUpdateOrDelete " + EntitiesNumber, lastEntity.Name);

            if (keepEntityItemId != null)
            {
                Assert.NotNull(context.Items.Where(x => x.ItemId == keepEntityItemId.Value).FirstOrDefault());
            }

            if (isBulk)
            {
                var bulkConfig = new BulkConfig()
                {
                    SetOutputIdentity = true, CalculateStats = true
                };
                bulkConfig.SetSynchronizeFilter <Item>(e => e.ItemId != keepEntityItemId.Value);
                context.BulkInsertOrUpdateOrDelete(new List <Item>(), bulkConfig);

                var storedEntities = context.Items.ToList();
                Assert.Single(storedEntities);
                Assert.Equal(3, storedEntities[0].ItemId);
            }
        }