You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Core EF如何从模型生成表?添加DbSet无效问题咨询

Understanding Entity Framework Core Code First & Migrations

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 a DbSet<T> property in your DbContext (like your MvcMovieContext) is automatically recognized.
  • Model configuration in OnModelCreating: Even if you don't add a DbSet<T>, if you configure the model using modelBuilder.Entity<YourModel>() inside the OnModelCreating method of your DbContext, 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 named Id or 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 using statement for your model's namespace in your DbContext file.
  • You need to generate a new migration (via Add-Migration <MigrationName>) after modifying your model or DbContext—Update-Database only 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:

  1. Snapshot creation: When you run Add-Migration, EF Core creates a snapshot of your current model state in YourContextModelSnapshot.cs (inside the Migrations folder). This snapshot acts as a reference point for future migrations.
  2. Migration generation: EF Core compares the current model (from your code) to the last snapshot. It generates a new migration file with Up() and Down() methods that contain the SQL needed to apply or revert the changes (like creating a table, adding a column, etc.).
  3. Applying migrations: Update-Database executes 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 __EFMigrationsHistory table 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 your DbContext for each model you want to include (this is the most common and readable approach).
  • Configure the model in OnModelCreating with modelBuilder.Entity<YourModel>()—this works even if you don't have a DbSet<T> for it.
  • Use assembly scanning: You can configure your DbContext to scan specific assemblies for model classes (useful if your models are in a separate project) by specifying the assembly in OnModelCreating or via context options.

内容的提问来源于stack exchange,提问作者aykl104001

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 03:42:45