Announcing Entity Framework 7 Preview 4

Jeremy Likness

Today, the .NET data team announces the fourth preview release of EF Core 7.0 (EF7). In addition to bug fixes and foundation work for larger features, this preview includes updates to ensure that converters and comparers are handled by type mapping and to support using converters with value generators. Be sure to read the full plan for EF7 to learn what’s on the roadmap.

Domain-driven design and “guarded” keys

One pattern from domain-driven design (DDD) is the concept of using value objects as keys. In other words, instead of declaring a key like this:

public class Thing
{
  public int Id { get; set; }
}

It might look like this instead:

public class Thing
{
  public ThingKey Id { get; set; }
}

public class ThingKey
{
  public ThingKey(Func<int> generator) => Id = generator();  
  public ThingKey(int id) => Id = id;
  public int Id { get; private set; }
}

There are several advantages to doing this. The first and perhaps most obvious is that it hides the implementation details of the key. If you decide that you’d prefer to consume the limitless resource of GUIDs instead the more limited and boringly sequential set of available integers, your refactoring will be much easier thanks to the buffer the key class provides. You are also able to guard the key from bad data with validations so someone doesn’t accidentally assign a negative value. Speaking of accidents, have you ever passed the wrong id to a method that takes a key? The compiler doesn’t care because it’s all ints … or bytes … or 1s and 0s … but if you’re using strongly-typed keys, you will be forced to provide the key that, cough “Dad Joke” cough* fits. Otherwise, the compiler will complain.

Whether or not you use this approach is of course up to you, but before preview 4 the choice was limited in EF Core. The most obvious way to translate from an entity to a scalar is to use a converter, but EF Core would throw an exception if you tried to assign a converter to a property flagged for value generation. That constraint has been lifted and more importantly, the code updated to ensure this case is handled properly. Thanks to the 81 of you who upvoted this issue, it was prioritized and is now ready for you to try out. Let us know if you’re using this feature and how it works for you!

Prerequisites

  • EF7 currently targets .NET 6. This will likely be updated to .NET 7 as we near the release.
  • EF7 will not run on .NET Framework.

EF7 is the successor to EF Core 6.0, not to be confused with EF6. If you are considering upgrading from EF6, please read our guide to port from EF6 to EF Core.


How to get EF7 previews

EF7 is distributed exclusively as a set of NuGet packages. For example, to add the SQL Server provider to your project, you can use the following command using the dotnet tool:

dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 7.0.0-preview.4.22229.2

This following table links to the preview 1 versions of the EF Core packages and describes what they are used for.

Package Purpose
Microsoft.EntityFrameworkCore The main EF Core package that is independent of specific database providers
Microsoft.EntityFrameworkCore.SqlServer Database provider for Microsoft SQL Server and SQL Azure
Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite SQL Server support for spatial types
Microsoft.EntityFrameworkCore.Sqlite Database provider for SQLite that includes the native binary for the database engine
Microsoft.EntityFrameworkCore.Sqlite.Core Database provider for SQLite without a packaged native binary
Microsoft.EntityFrameworkCore.Sqlite.NetTopologySuite SQLite support for spatial types
Microsoft.EntityFrameworkCore.Cosmos Database provider for Azure Cosmos DB
Microsoft.EntityFrameworkCore.InMemory The in-memory database provider
Microsoft.EntityFrameworkCore.Tools EF Core PowerShell commands for the Visual Studio Package Manager Console; use this to integrate tools like scaffolding and migrations with Visual Studio
Microsoft.EntityFrameworkCore.Design Shared design-time components for EF Core tools
Microsoft.EntityFrameworkCore.Proxies Lazy-loading and change-tracking proxies
Microsoft.EntityFrameworkCore.Abstractions Decoupled EF Core abstractions; use this for features like extended data annotations defined by EF Core
Microsoft.EntityFrameworkCore.Relational Shared EF Core components for relational database providers
Microsoft.EntityFrameworkCore.Analyzers C# analyzers for EF Core

We also published the 7.0 preview 1 release of the Microsoft.Data.Sqlite.Core provider for ADO.NET.

Installing the EF7 Command Line Interface (CLI)

Before you can execute EF7 Core migration or scaffolding commands, you’ll have to install the CLI package as either a global or local tool.

To install the preview tool globally, install with:

dotnet tool install --global dotnet-ef --version 7.0.0-preview.4.22229.2 

If you already have the tool installed, you can upgrade it with the following command:

dotnet tool update --global dotnet-ef --version 7.0.0-preview.4.22229.2 

It’s possible to use this new version of the EF7 CLI with projects that use older versions of the EF Core runtime.

Daily builds

EF7 previews are aligned with .NET 7 previews. These previews tend to lag behind the latest work on EF7. Consider using the daily builds instead to get the most up-to-date EF7 features and bug fixes.

As with the previews, the daily builds require .NET 6.

The .NET Data Community Standup

The .NET data team is now live streaming every other Wednesday at 10am Pacific Time, 1pm Eastern Time, or 17:00 UTC. Join the stream to ask questions about the data-related topic of your choice, including the latest preview release.

Documentation and Feedback

The starting point for all EF Core documentation is docs.microsoft.com/ef/.

Please file issues found and any other feedback on the dotnet/efcore GitHub repo.

The following links are provided for easy reference and access.

Thank you from the team

A big thank you from the EF team to everyone who has used and contributed to EF over the years!

Welcome to EF7.

3 comments

Discussion is closed. Login to edit/delete existing comments.

  • Fillipe Meireles 0

    🤩

  • Ntethelelo Mashabane 0

    Hi I have a question, when it comes to working with hierarchical partition keys in Entity Framework. The hierarchical partitions are defined in the cosmos database, but I am struggling to work with them in EF when I have to specify HasPartitionKey method. I get an error that my partition specified does not match the partition defined in the document. Please assist on how I can work with hierarchical partitions in EF. See code below:

    modelBuilder.Entity(entity =>
    {
    entity.HasNoDiscriminator();
    entity.ToContainer((“Document”));
    entity.HasPartitionKey(x => new {x.ModelId1, x.ModelId2);
    entity.HasKey(x => new { x.id });
    });

    • Jeremy LiknessMicrosoft employee 0

      Please file a new issue on the EF Core repo. That is where the team triages and prioritizes issues. Thanks!

      Jeremy

Feedback usabilla icon