text stringlengths 0 13.4k |
|---|
{ |
} |
protected override void OnModelCreating(ModelBuilder builder) |
{ |
base.OnModelCreating(builder); |
// ... |
} |
} |
Add a DbSet property to the ApplicationDbContext , right below the |
constructor: |
public ApplicationDbContext( |
DbContextOptions<ApplicationDbContext> options) |
: base(options) |
{ |
} |
public DbSet<TodoItem> Items { get; set; } |
// ... |
51 |
Update the context |
A DbSet represents a table or collection in the database. By creating a |
DbSet<TodoItem> property called Items , you're telling Entity |
Framework Core that you want to store TodoItem entities in a table |
called Items . |
You've updated the context class, but now there's one small problem: the |
context and database are now out of sync, because there isn't actually an |
Items table in the database. (Just updating the code of the context class |
doesn't change the database itself.) |
In order to update the database to reflect the change you just made to |
the context, you need to create a migration. |
If you already have an existing database, search the web for |
"scaffold-dbcontext existing database" and read Microsoft's |
documentation on using the Scaffold-DbContext tool to reverse- |
engineer your database structure into the proper DbContext and |
model classes automatically. |
52 |
Create a migration |
Create a migration |
Migrations keep track of changes to the database structure over time. |
They make it possible to undo (roll back) a set of changes, or create a |
second database with the same structure as the first. With migrations, |
you have a full history of modifications like adding or removing columns |
(and entire tables). |
In the previous chapter, you added an Items set to the context. Since |
the context now includes a set (or table) that doesn't exist in the |
database, you need to create a migration to update the database: |
dotnet ef migrations add AddItems |
This creates a new migration called AddItems by examining any changes |
you've made to the context. |
If you get an error like No executable found matching command |
"dotnet-ef" , make sure you're in the right directory. These |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.