ASP.NET Core中使用EntityFrameworkCore所需NuGet包的选择疑问
Great question! It makes total sense to be confused coming from EF6 (the old Entity Framework in ASP.NET MVC 5), where a single package covered everything. EF Core is built with modularity in mind, so packages are split up based on functionality—but you don't need to install every single one you see mentioned. Let's break down exactly what you need and why:
核心依赖包(必装)
These are the foundational packages your application needs to run EF Core with SQL Server:
Microsoft.EntityFrameworkCore: This is EF Core's core library, containing essential components likeDbContext,DbSet, LINQ query support, and base database provider abstractions. You can't use EF Core at all without this.Microsoft.EntityFrameworkCore.SqlServer: This is the SQL Server-specific database provider. It includes all code needed to connect to SQL Server, generate SQL tailored for it, and handle SQL Server-specific features (like identity columns, stored procedures, etc.). Since you're using SQL Server, this is non-negotiable.
开发工具包(仅开发环境需要)
If you want to use EF Core migrations, scaffold databases, or run EF-related commands (like Add-Migration/Update-Database in Package Manager Console, or dotnet ef via .NET CLI), you need this:
Microsoft.EntityFrameworkCore.Tools: This package adds EF Core command-line tools to your project. As you discovered, this package automatically pulls inMicrosoft.EntityFrameworkCore.Designas a dependency—you don't need to install Design separately. The Design package handles behind-the-scenes work like generating migration code and scaffolding models, but Tools wraps it up into usable commands for developers.
场景总结
- For development: Install the three packages above:
Microsoft.EntityFrameworkCore,Microsoft.EntityFrameworkCore.SqlServer, andMicrosoft.EntityFrameworkCore.Tools. That's all you need to write EF Core code, create migrations, and update your SQL Server database. - For production deployment: You only need
Microsoft.EntityFrameworkCoreandMicrosoft.EntityFrameworkCore.SqlServer. The Tools package is for development workflows and doesn't need to be included in your production build. Most developers configure Tools as a development-only dependency (using<PrivateAssets>all</PrivateAssets>in the .csproj file) to ensure it doesn't get deployed.
A quick note on Microsoft.EntityFrameworkCore.Relational: This is a base package for relational database providers (like SQL Server, PostgreSQL, MySQL) and is already included as a dependency of Microsoft.EntityFrameworkCore.SqlServer, so you don't need to install it manually.
内容的提问来源于stack exchange,提问作者KA-Yasso

