.NET Core EF如何从模型生成表?添加DbSet无效问题咨询
Hey there! Let's break down your questions about EF Core Code First and how migrations work, since you're just getting started with .NET Core:
1. How does Update-Database know which models to sync to the database?
EF Core identifies models to include in your database through a few key mechanisms:
- Explicit
DbSet<T>declarations: Any model you add as aDbSet<T>property in yourDbContext(like yourMvcMovieContext) is automatically recognized. - Model configuration in
OnModelCreating: Even if you don't add aDbSet<T>, if you configure the model usingmodelBuilder.Entity<YourModel>()inside theOnModelCreatingmethod of yourDbContext, EF Core will include it. - Automatic discovery: If your model classes are in the same assembly as your
DbContext(or you've configured EF to scan other assemblies), EF Core will detect classes with a primary key (usually a property namedIdor annotated with[Key]) as entities to include.
If adding a DbSet<T> didn't work for you, here are common fixes:
- Double-check that your model has a primary key (EF Core requires this to track entities).
- Make sure you've added the correct
usingstatement for your model's namespace in yourDbContextfile. - You need to generate a new migration (via
Add-Migration <MigrationName>) after modifying your model orDbContext—Update-Databaseonly applies existing migration files, not your current code directly.
2. Are Migrations the key here? What's the exact mechanism?
Yes, the Migrations folder is central to how EF Core syncs your model to the database. Here's the step-by-step flow:
- Snapshot creation: When you run
Add-Migration, EF Core creates a snapshot of your current model state inYourContextModelSnapshot.cs(inside the Migrations folder). This snapshot acts as a reference point for future migrations. - Migration generation: EF Core compares the current model (from your code) to the last snapshot. It generates a new migration file with
Up()andDown()methods that contain the SQL needed to apply or revert the changes (like creating a table, adding a column, etc.). - Applying migrations:
Update-Databaseexecutes all pending migration files (those not yet applied to the database) in order. It also keeps track of which migrations have been applied in the__EFMigrationsHistorytable in your database.
So Update-Database doesn't read your current model directly—it relies on the migration files that were generated when you ran Add-Migration.
3. Is scaffolding required? Is there a list to specify which models go into the database?
Scaffolding is completely optional—it's just a tool to quickly generate controllers and views for CRUD operations. You don't need it at all if you don't need a UI for your database tables.
To specify which models are included in the database, you have these options (no "magic list" exists, but these are the explicit ways):
- Add a
DbSet<T>property to yourDbContextfor each model you want to include (this is the most common and readable approach). - Configure the model in
OnModelCreatingwithmodelBuilder.Entity<YourModel>()—this works even if you don't have aDbSet<T>for it. - Use assembly scanning: You can configure your
DbContextto scan specific assemblies for model classes (useful if your models are in a separate project) by specifying the assembly inOnModelCreatingor via context options.
内容的提问来源于stack exchange,提问作者aykl104001

