Entity Framework 6

repository·main·Indexed 23 days ago

https://github.com/dotnet/ef6

A mature object-relational mapper (ORM) for .NET that maps objects to relational database structures. EF6 is currently in maintenance mode, focusing on stability and security fixes for .NET Framework and .NET Core applications. It supports Visual Designer and EDMX files, and includes providers for SQL Server Compact 4.0 and a modern Microsoft.Data.SqlClient-based SQL Server provider.

Tokens
4.9K
Snippets
6
Records
28
Agent score
80%

What's inside EF6

  1. Overview of Entity Framework 6

    main
    Entity Framework 6 (EF6) is an object-relational mapper (ORM) for .NET. It is designed to eliminate the need for much of the boilerplate data-access code typically required when interacting with databases by mapping objects to relational database structures.
  2. Choose between EF6 and EF Core

    main

    When deciding whether to use EF6 or EF Core, consider the following differences:

    FeatureEntity Framework 6 (EF6)Entity Framework Core (EF Core)
    Development StatusMaintenance mode (stable)Actively developed
    Target Runtime.NET Framework and .NET CoreModern .NET only (does not run on .NET Framework)
    ArchitectureSupports Visual Designer and EDMX filesLightweight, extensible, no Visual Designer/EDMX
    PerformanceEstablished/StableIncludes many improvements and new features

    If you are starting a new project on modern .NET, EF Core is the recommended path. If you have an existing EF6 project, you can follow the Port from EF6 to EF Core guide to migrate.

  3. Understand the support status of EF6

    main

    EF6 is in a maintenance mode and is no longer under active feature development.

    What to expect:

    • Security: Security issues will be fixed.
    • Bugs: High-impact bugs (affecting a large number of users) may be fixed, but most other bugs will not be addressed.
    • Features: No new features will be implemented.
    • Stability: The focus is on codebase stability and compatibility with new versions of the .NET ecosystem.
    • Contributions: Community pull requests are not being accepted to ensure maximum stability.
  4. Use the provider with EDMX files

    main

    To use this provider with an EDMX model, you must update the provider name in two locations.

    Warning: To use the Visual Studio designer, you must temporarily switch the provider name back to System.Data.SqlClient.

    <!-- 1. Update the StorageModel Schema Provider -->
    <edmx:Edmx Version="3.0" xmlns:edmx="http://schemas.microsoft.com/ado/2009/11/edmx">
      <edmx:Runtime>
        <edmx:StorageModels>
          <Schema Namespace="ChinookModel.Store" Provider="Microsoft.Data.SqlClient" >
    
    <!-- 2. Update the EntityConnection connection string provider -->
     <add 
        name="Database" 
        connectionString="metadata=res://*/EFModels.csdl|res://*/EFModels.ssdl|res://*/EFModels.msl;provider=Microsoft.Data.SqlClient;provider connection string=\"data source=server;initial catalog=mydb;integrated security=True;persist security info=True;\"" 
        providerName="System.Data.EntityClient" 
     />
  5. Reduce security scan overhead using Multiple Outputs

    main

    When using 1ES pipeline templates (templates-official), every publish artifact execution triggers additional security scans. To reduce this overhead, you can use the outputParentDirectory feature to gather all publishing outputs into the $(Build.ArtifactStagingDirectory) and utilize the templateContext.outputs parameter.

    Implementation Steps:

    1. Ensure all build artifacts are copied to $(Build.ArtifactStagingDirectory) using a task like CopyFiles@2.
    2. Define your outputs within the templateContext block in your job template.

    Note: Multiple outputs are only applicable to 1ES PT publishing (when referencing templates-official).

    # azure-pipelines.yml
    extends:
      template: azure-pipelines/MicroBuild.1ES.Official.yml@MicroBuildTemplate
      parameters:
        stages:
        - stage: build
          jobs:
          - template: /eng/common/templates-official/jobs/jobs.yml@self
            parameters:
              # 1ES makes use of outputs to reduce security task injection overhead
              templateContext:
                outputs:
                - output: pipelineArtifact
                  displayName: 'Publish logs from source'
                  continueOnError: true
                  condition: always()
                  targetPath: $(Build.ArtifactStagingDirectory)/artifacts/log
                  artifactName: Logs
              jobs:
              - job: Windows
                steps:
                - script: echo "friendly neighborhood" > artifacts/marvel/spiderman.txt
              # copy build outputs to artifact staging directory for publishing
              - task: CopyFiles@2
                  displayName: Gather build output
                  inputs:
                    SourceFolder: '$(System.DefaultWorkingDirectory)/artifacts/marvel'
                    Contents: '**'
                    TargetFolder: '$(Build.ArtifactStagingDirectory)/artifacts/marvel'
  6. Install the Entity Framework 6 SQL Server provider

    main

    This provider is a replacement for the built-in SQL Server provider in Entity Framework 6. It is based on the modern Microsoft.Data.SqlClient ADO.NET provider, offering support for SQL Server 2022 (TDS8), Azure Active Directory authentication, and Always Encrypted.

    Note: This is a runtime-only update and is not compatible with existing Visual Studio tooling (like the EDMX designer).

  7. Migrate code to the Microsoft.Data.SqlClient provider

    main

    When switching from the built-in provider to this one, perform the following updates in your codebase:

    Namespace Changes

    • Replace using System.Data.SqlClient; with using Microsoft.Data.SqlClient;
    • Replace using Microsoft.SqlServer.Server; with using Microsoft.Data.SqlClient.Server;

    Renamed Classes

    The following classes have been renamed to avoid conflicts with the legacy System.Data.SqlClient provider:

    Old NameNew Name
    SqlAzureExecutionStrategyMicrosoftSqlAzureExecutionStrategy
    SqlDbConfigurationMicrosoftSqlDbConfiguration
    SqlProviderServicesMicrosoftSqlProviderServices
    SqlServerMigrationSqlGeneratorMicrosoftSqlServerMigrationSqlGenerator
    SqlSpatialServicesMicrosoftSqlSpatialServices
    SqlConnectionFactoryMicrosoftSqlConnectionFactory
    LocalDbConnectionFactoryMicrosoftLocalDbConnectionFactory
  8. Configure the Microsoft SQL Server provider in code

    main

    You can register the new provider using one of the following three methods:

    1. Using the [DbConfigurationType] attribute

    Apply this attribute to your DbContext class. If you have multiple DbContext classes, you must apply it to all of them.

    2. Using DbConfiguration.SetConfiguration

    Call this method before any data access occurs in your application.

    3. Extending an existing DbConfiguration class

    If you already have a custom DbConfiguration class, add the following lines to map the provider names to the Microsoft.Data.SqlClient implementation.

  9. Use Entity Framework Migrations cmdlets

    main

    Entity Framework Migrations allow you to manage database schema changes using a Code First approach. Use the following cmdlets within the Package Manager Console to manage your migrations lifecycle:

    • Enable-Migrations: Initializes Code First Migrations in your project.
    • Add-Migration <MigrationName>: Scaffolds a new migration script based on any pending changes in your model.
    • Update-Database: Applies any pending migrations to the target database.
    • Get-Migrations: Lists the migrations that have already been applied to the target database.