public static void Initialize(IServiceProvider serviceProvider) { DbContextOptions <ToDoListContext> dbContextOptions = serviceProvider.GetRequiredService <DbContextOptions <ToDoListContext> >(); var context = new ToDoListContext(dbContextOptions); using (context) { if (context.ToDoItems.Any()) { return; } context.ToDoItems.AddRange( new ToDoItem { Name = "Read newsletters", IsComplete = false }, new ToDoItem { Name = "Study Algorithms", IsComplete = true }, new ToDoItem { Name = "Yoga", IsComplete = false } ); context.SaveChanges(); } }
public static void Seed(ToDoListContext context) { if (!context.ToDoItems.Any()) { context.ToDoItems.Add(new ToDoItem("Sample Task", Priority.Medium)); context.SaveChanges(); } }
public static void Initialize(IServiceProvider serviceProvider) { using (var context = new ToDoListContext( serviceProvider.GetRequiredService <DbContextOptions <ToDoListContext> >())) { // Look for any movies. if (context.SomeTask.Any()) { Console.WriteLine("DB already has records"); return; // DB has been seeded } context.SomeTask.AddRange( new SomeTask { Id = 1, Title = "Clean Room", Description = "Put stuff away. Clean dust. Sweep floor.", Status = false }, new SomeTask { Id = 2, Title = "Make App", Description = "Figure out how to make the app. Why is C# confusing you?", Status = false }, new SomeTask { Id = 3, Title = "Make Dinner", Description = "Get required groceries. Clean dishes. Make dinner. Clean dishes again.", Status = true }, new SomeTask { Id = 4, Title = "Be Lazy", Description = "Accomplish nothing. Procrastinate responsibilities.", Status = true } ); context.SaveChanges(); } }
public static void Initialize(IServiceProvider serviceProvider) { using (var context = new ToDoListContext( serviceProvider.GetRequiredService < DbContextOptions <ToDoListContext> >())) { if (context.Items.Any()) { return; } context.Items.AddRange( new Item { Title = "Доделать TODOList", ExecutionDate = DateTime.Parse("2021-2-10"), Description = "Наконецто сделать его и сдать.", IsDone = false }, new Item { Title = "Добавить новый пункт ", ExecutionDate = DateTime.Parse("2021-2-10"), Description = "Добавить в список новый пункт и выполнить его.", IsDone = false }, new Item { Title = "Покушать", ExecutionDate = DateTime.Parse("2021-2-10"), Description = "Круто пожрать", IsDone = true } ); context.SaveChanges(); } }
public static void Initialize(IServiceProvider serviceProvider) { using (var context = new ToDoListContext( serviceProvider.GetRequiredService < DbContextOptions <ToDoListContext> >())) { // Look for any movies. if (context.ToDoTask.Any()) { return; // DB has been seeded } context.ToDoTask.AddRange( new ToDoTask { Title = "Add New Tasks!", DueDate = DateTime.Parse("2020-01-01") } ); context.SaveChanges(); } }
public Item Save(Item item) { db.Items.Add(item); db.SaveChanges(); return(item); }
public Item Edit(Item item) { db.Entry(item).State = Microsoft.EntityFrameworkCore.EntityState.Modified; db.SaveChanges(); return(item); }