Skills2026年5月9日·1 分钟阅读

Entity Framework Core — Modern ORM for .NET Applications

Entity Framework Core is the official open-source ORM for .NET that enables developers to work with databases using C# objects and LINQ queries instead of raw SQL.

Agent 就绪

这个资产可以被 Agent 直接读取和安装

TokRepo 同时提供通用 CLI 命令、安装契约、metadata JSON、按适配器生成的安装计划和原始内容链接,方便 Agent 判断适配度、风险和下一步动作。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
EF Core Overview
通用 CLI 安装命令
npx tokrepo install 2a47b1ea-4ba2-11f1-9bc6-00163e2b0d79

Introduction

Entity Framework Core (EF Core) is Microsoft's lightweight, extensible ORM for .NET. It maps C# classes to database tables and translates LINQ expressions into SQL, supporting multiple database providers including SQL Server, PostgreSQL, SQLite, and MySQL.

What EF Core Does

  • Maps .NET classes to relational tables with convention-based and fluent configuration
  • Translates LINQ queries to optimized provider-specific SQL at runtime
  • Manages schema evolution through code-first migrations
  • Tracks entity state changes and batches INSERT/UPDATE/DELETE in a single round-trip
  • Supports both relational and document databases via pluggable providers

Architecture Overview

EF Core uses a DbContext as the unit-of-work entry point. Models are discovered by convention or configured via the Fluent API. A change tracker monitors entity states (Added, Modified, Deleted). On SaveChanges, the query pipeline translates the expression tree to SQL using the active database provider. Providers (Npgsql for PostgreSQL, Pomelo for MySQL, etc.) handle dialect differences. Compiled queries and connection pooling reduce overhead in high-throughput scenarios.

Self-Hosting & Configuration

  • Install the core NuGet package plus a provider package (e.g., Npgsql.EntityFrameworkCore.PostgreSQL)
  • Register the DbContext in the .NET dependency injection container with connection string
  • Generate and apply migrations with the dotnet ef CLI tool
  • Use DbContextOptionsBuilder to configure connection pooling, retry logic, and logging
  • Enable compiled models for startup performance in large schemas

Key Features

  • Code-first migrations with rollback and SQL script generation
  • Global query filters for soft-delete and multi-tenancy patterns
  • Lazy, eager, and explicit loading strategies for navigation properties
  • Raw SQL and interpolated SQL support for complex queries with parameter safety
  • Interceptors for logging, auditing, and cross-cutting concerns at the database layer

Comparison with Similar Tools

  • Dapper — Micro-ORM with manual SQL; EF Core offers full change tracking and migrations
  • SQLAlchemy — Python ORM with similar unit-of-work; EF Core is native to the .NET ecosystem
  • Hibernate — Java ORM counterpart; EF Core uses LINQ instead of HQL/JPQL
  • Prisma — TypeScript ORM with schema-first design; EF Core supports both code-first and DB-first
  • Django ORM — Tightly coupled to Django; EF Core works with any .NET application

FAQ

Q: Can EF Core be used without migrations? A: Yes. You can reverse-engineer an existing database with dotnet ef dbcontext scaffold for database-first workflows.

Q: Does EF Core support NoSQL databases? A: The Azure Cosmos DB provider is available. Community providers exist for MongoDB.

Q: How do I optimize EF Core query performance? A: Use projection (Select), split queries for collections, AsNoTracking for read-only data, and compiled queries for hot paths.

Q: Is EF Core suitable for high-throughput workloads? A: Yes, with proper configuration. Batch operations, compiled models, and DbContext pooling significantly improve performance.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产