text
stringlengths
0
13.4k
in the Up method.
Comment out or remove any migrationBuilder.DropForeignKey lines
in the Down method.
If you use a full-fledged SQL database, like SQL Server or MySQL, this
won't be an issue and you won't need to do this (admittedly hackish)
workaround.
Apply the migration
The final step after creating one (or more) migrations is to actually apply
them to the database:
dotnet ef database update
This command will cause Entity Framework Core to create the Items
table in the database.
55
Create a migration
If you want to roll back the database, you can provide the name of
the previous migration: dotnet ef database update
CreateIdentitySchema This will run the Down methods of any
migrations newer than the migration you specify.
If you need to completely erase the database and start over, run
dotnet ef database drop followed by dotnet ef database update
to re-scaffold the database and bring it up to the current
migration.
That's it! Both the database and the context are ready to go. Next, you'll
use the context in your service layer.
56
Create a new service class
Create a new service class
Back in the MVC basics chapter, you created a FakeTodoItemService that
contained hard-coded to-do items. Now that you have a database
context, you can create a new service class that will use Entity
Framework Core to get the real items from the database.
Delete the FakeTodoItemService.cs file, and create a new file:
Services/TodoItemService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AspNetCoreTodo.Data;
using AspNetCoreTodo.Models;
using Microsoft.EntityFrameworkCore;
namespace AspNetCoreTodo.Services
{
public class TodoItemService : ITodoItemService
{
private readonly ApplicationDbContext _context;
public TodoItemService(ApplicationDbContext context)
{
_context = context;
}
public async Task<TodoItem[]> GetIncompleteItemsAsync()