コード例 #1
0
        public async Task RemovesSql()
        {
            var toDelete = SqlIds.Skip(5).Take(5).ToList();

            using (var db = new woodmanContext())
            {
                var entities = await db.EfCoreTest
                               .Where(e => toDelete.Contains(e.Id))
                               .ToListAsync();

                Assert.NotEmpty(entities);
            }

            using (var db = new woodmanContext())
            {
                var result = await db.EfCoreTest
                             .Where(e => e.Name.Contains(nameof(BulkRemoveTests)))
                             .BulkRemoveAsync(toDelete);

                Assert.Equal(5, result);
            }

            using (var db = new woodmanContext())
            {
                var entities = await db.EfCoreTest
                               .Where(e => toDelete.Contains(e.Id))
                               .ToListAsync();

                Assert.Empty(entities);
            }
        }
コード例 #2
0
        public async Task UpdatesSql()
        {
            var toUpdate = SqlIds.Take(2)
                           .ToDictionary(key => key, value => DateTime.UtcNow.AddDays(value));

            using (var db = new woodmanContext())
            {
                var result = await db.EfCoreTest.BulkUpdateAsync(toUpdate.Select(x => x.Key), id => new EfCoreTest
                {
                    ModifiedDate = toUpdate[id]
                });

                Assert.Equal(toUpdate.Count, result);
            }

            using (var db = new woodmanContext())
            {
                var updated = await db.EfCoreTest
                              .Where(x => toUpdate.Select(y => y.Key).Contains(x.Id))
                              .ToListAsync();

                Assert.Equal(toUpdate.Count, updated.Count);

                foreach (var u in updated)
                {
                    Assert.Contains(u.Id, toUpdate.Select(x => x.Key));

                    var expected = toUpdate[u.Id];
                    var actual   = u.ModifiedDate;

                    Assert.Equal(expected.ToString("G"), actual.ToString("G"));
                }
            }
        }
コード例 #3
0
        public async Task JoinsByPrimaryKeySql()
        {
            var expectedIds = SqlIds.Take(5).ToList();

            List <int> actualIds;

            using (var db = new woodmanContext())
            {
                actualIds = await db.EfCoreTest
                            .Join(expectedIds)
                            .Select(e => e.Id)
                            .ToListAsync();
            }

            Assert.Equal(expectedIds.Count, actualIds.Count);

            foreach (var expected in expectedIds)
            {
                Assert.Contains(expected, actualIds);
            }
        }
コード例 #4
0
        public async Task UpdatesWithoutKeysSql()
        {
            var createdDate = DateTime.UtcNow;

            var toUpdate = SqlIds.Take(2).ToList();

            using (var db = new woodmanContext())
            {
                var result = await db.EfCoreTest
                             .Where(x => toUpdate.Contains(x.Id))
                             .BulkUpdateAsync(() => new EfCoreTest
                {
                    CreatedDate = createdDate
                });

                Assert.Equal(toUpdate.Count, result);
            }

            using (var db = new woodmanContext())
            {
                var updated = await db.EfCoreTest
                              .Where(x => toUpdate.Contains(x.Id))
                              .ToListAsync();

                Assert.Equal(toUpdate.Count, updated.Count);

                foreach (var u in updated)
                {
                    Assert.Contains(u.Id, toUpdate);

                    var expected = createdDate;
                    var actual   = u.CreatedDate;

                    Assert.Equal(expected.ToString("G"), actual.ToString("G"));
                }
            }
        }