コード例 #1
0
        public static async void Seed(IServiceProvider serviceProvider)
        {
            // Style to enable extension
            var contextOptions = serviceProvider.GetRequiredService <DbContextOptions <TodoDbContext> >();

            using (var context = new TodoDbContext(contextOptions))
            {
                if (await context.Todos.AnyAsync())
                {
                    return;
                }

                foreach (var i in Enumerable.Range(1, 10))
                {
                    await context.Todos.AddAsync(
                        new TodoSet { ID = i, Title = $"Todo item {i}", IsCompleted = false }
                        );
                }

                await context.SaveChangesAsync();
            }
        }
コード例 #2
0
 public TodoRepository(TodoDbContext todoDbContext)
 {
     _todoDbContext = todoDbContext;
 }