Example #1
0
        private static async Task DropTableAsync(MonsterDbContext context)
        {
            var sql = @"
drop table if exists dbo.Monster;
drop table if exists dbo.MonsterCategory;";
            await context.Database.ExecuteSqlRawAsync(sql);
        }
Example #2
0
        private static async Task InitTableAsync(MonsterDbContext context)
        {
            var monsterCategories = new[] {
                new MonsterCategory {
                    Type = MonsterCategoryType.Slime,
                    Name = "スライム系",
                },
                new MonsterCategory {
                    Type = MonsterCategoryType.Animal,
                    Name = "けもの系",
                },
                new MonsterCategory {
                    Type = MonsterCategoryType.Fly,
                    Name = "鳥系",
                },
            };

            var monsters = new[] {
                new Monster {
                    Id           = 1,
                    Name         = "スライム",
                    CategoryType = MonsterCategoryType.Slime,
                },
                new Monster {
                    Id           = 2,
                    Name         = "ドラキー",
                    CategoryType = MonsterCategoryType.Fly,
                },
            };

            context.MonsterCategories.AddRange(monsterCategories);
            context.Monsters.AddRange(monsters);

            await context.SaveChangesAsync();
        }
Example #3
0
        public async Task 主キーや外部キーのID列をEnumにバインドできてインクルードもできる()
        {
            using var context = new MonsterDbContext();

            try {
                await DropTableAsync(context);
                await CreateTableAsync(context);
                await InitTableAsync(context);

                var monster = await context.Monsters
                              // カテゴリをインクルード
                              .Include(monster => monster.Category)
                              .FirstOrDefaultAsync(monster => monster.CategoryType == MonsterCategoryType.Fly);

                Assert.NotNull(monster);
                Assert.Equal(2, monster.Id);
                Assert.Equal("ドラキー", monster.Name);
                Assert.Equal(MonsterCategoryType.Fly, monster.CategoryType);
                Assert.Equal(MonsterCategoryType.Fly, monster.Category.Type);
                Assert.Equal("鳥系", monster.Category.Name);
            } catch (Exception exception) {
                _output.WriteLine(exception.ToString());
                AssertHelper.Fail();
            } finally {
                await DropTableAsync(context);
            }
        }
Example #4
0
        public async Task 主キーや外部キーのID列をEnumにバインドできる()
        {
            using var context = new MonsterDbContext();

            try {
                await DropTableAsync(context);
                await CreateTableAsync(context);
                await InitTableAsync(context);

                var monsters = await context.Monsters
                               .Where(monster => monster.CategoryType == MonsterCategoryType.Slime)
                               .ToListAsync();

                Assert.Single(monsters);

                var monster = monsters.First();
                Assert.Equal(1, monster.Id);
                Assert.Equal("スライム", monster.Name);
                Assert.Equal(MonsterCategoryType.Slime, monster.CategoryType);
                Assert.Null(monster.Category);
            } catch (Exception exception) {
                _output.WriteLine(exception.ToString());
                AssertHelper.Fail();
            } finally {
                await DropTableAsync(context);
            }
        }
Example #5
0
        public RelatedDataTest()
        {
            _context = new MonsterDbContext();

            DropTable();
            CreateTable();
        }
Example #6
0
        public void Dispose()
        {
            DropTable();

            if (_context != null)
            {
                _context.Dispose();
                _context = null;
            }
        }
Example #7
0
        private static async Task CreateTableAsync(MonsterDbContext context)
        {
            var sql = @"
create table dbo.MonsterCategory(
	Id int,
	Name nvarchar(20),
	constraint PK_MonsterCategory primary key(Id)
);
create table dbo.Monster(
	Id int,
	Name nvarchar(20),
	CategoryId int,
	constraint PK_Monster primary key(Id),
	constraint FK_Monster_MonsterCategory foreign key(CategoryId)
		references dbo.MonsterCategory(Id)
);";
            await context.Database.ExecuteSqlRawAsync(sql);
        }