Пример #1
0
        private static void SaveCategories(IEnumerable <Category> categories, string connectionString = null)
        {
            var options = new DbContextOptionsBuilder <RunnerDbContext>()
                          .UseSqlServer(connectionString ?? "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MarathonManager;Integrated Security=True")
                          .Options;

            using (var context = new RunnerDbContext(options))
            {
                context.AddRange(categories.ToList());
                context.SaveChanges();
            }
        }
Пример #2
0
        private static void InsertCategories()
        {
            var defaultCategories = ReadCategories();

            var options = new DbContextOptionsBuilder <RunnerDbContext>()
                          .UseSqlServer(Configuration.GetConnectionString("Default"))
                          .Options;

            using (var context = new RunnerDbContext(options))
            {
                var categories = defaultCategories.Select(c => new Category {
                    Name = c.Name
                });
                context.AddRange(categories);
                context.SaveChanges();

                WriteLine("Categories were successfully saved:");
                categories.ForEach(c => WriteLine($"| {c.Name}"));
            }
        }