Entity Framework Documentation

repository·main·Indexed 23 days ago

https://github.com/dotnet/entityframework.docs

Official documentation source files for Entity Framework (EF Core and EF6) hosted on Microsoft Learn. Includes guides on explicit entity tracking, compiled models via the EF Core CLI, keyless entity types for database views, and DbContext pooling. Provides sample applications such as Planetary Docs, demonstrating CRUD operations with Blazor, EF Core, and Azure Cosmos DB.

Tokens
238.4K
Snippets
486
Records
1.1K
Agent score
82%

What's inside dotnet-entityframework.docs

  1. Overview of Entity Framework 6 Validation

    main

    Entity Framework 6 (EF6) provides server-side validation that complements client-side validation (such as in ASP.NET MVC). EF6 automatically validates entities based on configurations applied via Data Annotations or the Fluent API.

    Validation can be implemented using:

    • Data Annotations: Attributes applied directly to model properties.
    • Fluent API: Configurations applied during model building.
    • IValidatableObject: An interface implemented by your entity classes for custom validation logic.
    • DbContext.ValidateEntity: An extensibility point within the DbContext to customize validation behavior.

    These validation mechanisms are available regardless of whether you use Code First, Model First, or Database First workflows.

  2. Browse .NET Data Community Standup recordings

    main

    The .NET Data Community Standups provide a collection of video recordings and presentations covering various topics related to Entity Framework Core, SQL Server, database design, and the broader .NET data ecosystem. You can use this summary to find specific deep-dives into topics such as:

    • EF Core Features: Migrations, Bulk Updates, Compiled Models, Property Mapping, and many-to-many relationships.
    • Database Technologies: SQL Server (Temporal tables, Azure SQL), PostgreSQL, SQLite, MySQL, MongoDB, and Vector databases.
    • Performance & Patterns: Query anti-patterns, context pooling, compiled queries, and concurrency.
    • Tools: EF Core Power Tools, SQL Database Project Power Tools, and various scaffolding options.
    • Architectural Patterns: DDD (Complex types), Repository patterns, and GraphQL integration.

    Refer to the summary table in the source documentation to navigate to specific years and topics.

  3. Reverse Engineering (Scaffolding) in EF Core

    main

    Reverse engineering (or scaffolding) is the process of generating entity type classes and a DbContext class based on an existing database schema.

    You can perform scaffolding using two different toolsets:

    1. .NET CLI tools: Works across all platforms supported by .NET using the dotnet ef dbcontext scaffold command.
    2. Package Manager Console (PMC) tools: Works only in Visual Studio using the Scaffold-DbContext command.

    Note: This is distinct from ASP.NET Core controller scaffolding. For a graphical interface in Visual Studio, consider the EF Core Power Tools extension.

  4. Project Architecture and Features of Planetary Docs

    main

    The Planetary Docs sample is divided into several specialized projects demonstrating different integration patterns:

    PlanetaryDocsLoader

    Parses the documentation repository and inserts documents into the database. Includes tests to verify functionality.

    PlanetaryDocs.Domain

    Contains domain classes, validation logic, and data access interfaces.

    PlanetaryDocs.DataAccess

    Implements the EF Core layer. Key technical demonstrations include:

    • DocsContext:
      • Model-building for ownership mapping.
      • Value converters with JSON serialization for primitive collections and nested complex types.
      • Partition key usage (definition in model and specification in queries).
      • Specifying containers by entity.
      • Disabling discriminators.
      • Storing multiple entity types (aliases and tags) in a single container.
      • Using "shadow properties" to track partition keys.
      • Automating audit snapshots via the SavingChanges event.
    • DocumentService:
      • CRUD operation strategies.
      • Programmatic synchronization of related entities.
      • Handling concurrency updates for disconnected entities.
      • Managing context instances using IDbContextFactory<T>.

    PlanetaryDocs (Blazor Server App)

    Demonstrates UI/UX patterns including:

    • JavaScript interop (via TitleService, HistoryService, and MultiLineEditService).
    • Keyboard handlers for navigation and input.
    • Generic autocomplete components with built-in debounce.
    • Markdown to HTML transformation using MarkDig.
    • Concurrency handling in the Editor component (notifying users of changes made in other tabs).
  5. Integrate EF Core with Aspire, HotChocolate, and GraphQL

    main

    Several API integration packages exist to extend EF Core functionality for cloud-native development and GraphQL endpoints:

    • Aspire: Simplifies configuration and interconnections for cloud-native apps. (Supports EF Core 8-10)
    • HotChocolate: Used to build GraphQL endpoints on top of resources.
    • GraphQL.EntityFramework: Adds IQueryable support to GraphQL. (Supports EF Core 6-8)
    • EntityGraphQL: Provides a GraphQL server with tight Entity Framework integration. (Supports EF Core 5-8)
    • OData: Implements REST APIs with support for discovery, filtering, sorting, projections, and more.
  6. New features in EF Core 8

    main

    Entity Framework Core 8 introduced several key features for improved data modeling and querying. Key highlights include:

    • Primitive collections: Support for collections of primitive types (e.g., List<int>) mapped to JSON columns in relational databases.
    • Complex types: A new way to model DDD value objects that do not have a key or identity but can be deconstructed into database columns.
    • HierarchyId: Support for hierarchical data structures (like trees) using HierarchyId in SQL Server or ltree in PostgreSQL.
    • Raw SQL queries for unmapped types: Ability to execute raw SQL and map results to types not explicitly part of the EF model.
    • Enhanced Lazy-loading: Improvements to how related data is loaded.
    • Improved JSON support: Better handling of JSON columns and queryable collections.
    • DateOnly/TimeOnly: Native support for these types on SQL Server.
    • Sentinel values: Control when the database generates default values.
    • Performance improvements: Significant optimizations for IN and EXISTS LINQ queries.
  7. Plan for Entity Framework Core 8

    main

    EF Core 8 (EF8) is a Long-Term Support (LTS) release scheduled for November 2023, aligning with .NET 8. It focuses on highly requested features, cloud-native capabilities (AOT/trimming), performance improvements, and enhanced visual tooling.

    Key Platform Details:

    • Target Framework: EF8 targets .NET 8 (previously .NET 6 in planning).
    • Compatibility: EF8 does not target .NET Standard and will not run on .NET Framework.
    • Breaking Changes: A small number of breaking changes are expected to evolve the platform.
  8. Explore .NET Data Community Standups

    main
    The .NET Data Community Standups are video sessions hosted by Jiri Cincura that cover a wide range of topics related to Entity Framework Core, database technologies, and the broader .NET data ecosystem. These sessions include deep dives into specific features (like temporal tables or vector search), comparisons between different data access approaches (like Dapper vs. EF Core), and discussions on best practices and anti-patterns. You can find recorded sessions organized by year (e.g., 2025, 2026) on YouTube.
  9. Choose between EF Core Package Manager Console tools and .NET CLI tools

    main

    Entity Framework Core tools assist with design-time tasks such as managing Migrations and scaffolding a DbContext and entity types via database reverse engineering. You can use one of two toolsets, which provide identical functionality:

    • EF Core Package Manager Console tools: These run within the Package Manager Console in Visual Studio. Use these if you are developing in Visual Studio for a more integrated experience.
    • EF Core .NET CLI tools: These are an extension to the cross-platform .NET CLI. These tools require a .NET SDK project (a project containing Sdk="Microsoft.NET.Sdk" or similar in the project file).
  10. What's New in EF Core 9

    main

    EF Core 9.0 (EF9) is a Short Term Support (STS) release available as of November 2024. It is supported until November 10, 2026. EF9 targets .NET 8 and is compatible with both .NET 8 (LTS) and .NET 9.

    Key feature areas introduced in EF9 include:

    • Azure Cosmos DB for NoSQL support
    • Improvements to querying with partition keys and document IDs
    • Hierarchical partition keys
    • Enhanced LINQ querying capabilities
    • Improved modeling for Azure Cosmos DB and JSON standards
    • Vector similarity search (preview)
    • Pagination support
    • Safer SQL querying via FromSql