头图

Recently the .NET Data team announced the fourth preview of EF Core 7.0 (EF7) . In addition to the groundwork for bug fixes and greater functionality, this preview also includes support to ensure converters and comparators are handled by typemaps , and support for using converters with value generators . Be sure to read the full plan for EF7 to see what's on the roadmap.

Domain-Driven Design and "Protected" Keys

One of the patterns of 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:

 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 benefits to doing so. The first and most obvious is that it hides the implementation details of the key. If you decide to prefer the infinite resource of GUIDs, rather than the more limited and boring sequential set of available integers, your refactoring will be easier thanks to the buffers provided by the key class. You can also protect the key from bad data with validation so that no one can assign a negative value. Speaking of accidents, have you ever passed the wrong id as a key to a method? It just doesn't matter to the compiler because it's all integers...or bytes...or 1s and 0s...but if you use strongly typed keys, you're forced to provide the appropriate keys. Otherwise, the compiler will complain.

Whether you use this method is up to you of course, but until Preview 4, options in EF Core were limited. The most obvious way to convert from an entity to a scalar is to use a converter, but EF Core will throw an exception if you try to assign a converter to a property marked as generating a value. The constraint has been lifted and, more importantly, the code has been updated to ensure this case is handled correctly. Let us know if you are using this feature and how it works for you!

prerequisites

EF7 currently targets .NET 6. It may be updated to .NET 7 as we're about to release it.

EF7 will not run on the .NET Framework.

EF7 is the successor to EF Core 6.0, not to be confused with EF6. If you're considering upgrading from EF6, read our guide on porting from EF6 to EF Core .

How to get an EF7 preview

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

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

The following table links to Preview 1 of the EF Core packages and describes their purpose.
image.png

  • Microsoft.EntityFrameworkCore

https://www.nuget.org/packages/Microsoft.EntityFrameworkCore/7.0.0-preview.4.22229.2

  • Microsoft.EntityFrameworkCore.SqlServer

https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.SqlServer/7.0.0-preview.4.22229.2

  • Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite

https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite/7.0.0-preview.4.22229.2

  • Microsoft.EntityFrameworkCore.Sqlite

https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Sqlite/7.0.0-preview.4.22229.2

  • Microsoft.EntityFrameworkCore.Sqlite.Core

https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Sqlite.Core/7.0.0-preview.4.22229.2

  • Microsoft.EntityFrameworkCore.Sqlite.NetTopologySuite

https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Sqlite.NetTopologySuite/7.0.0-preview.4.22229.2

  • Microsoft.EntityFrameworkCore.Cosmos

https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Cosmos/7.0.0-preview.4.22229.2

  • Microsoft.EntityFrameworkCore.InMemory

https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.InMemory/7.0.0-preview.4.22229.2

  • Microsoft.EntityFrameworkCore.Tools

https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Tools/7.0.0-preview.4.22229.2

  • Microsoft.EntityFrameworkCore.Design

https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Design/7.0.0-preview.4.22229.2

  • Microsoft.EntityFrameworkCore.Proxies

https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Proxies/7.0.0-preview.4.22229.2

  • Microsoft.EntityFrameworkCore.Abstractions

https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Abstractions/7.0.0-preview.4.22229.2

  • Microsoft.EntityFrameworkCore.Relational

https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Relational/7.0.0-preview.4.22229.2

  • Microsoft.EntityFrameworkCore.Analyzers

https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Analyzers/7.0.0-preview.4.22229.2

  • scaffold

https://docs.microsoft.com/ef/core/managing-schemas/scaffolding

  • migrate

https://docs.microsoft.com/ef/core/managing-schemas/migrations/

We also released Microsoft.Data.Sqlite.Core 7.0 Preview 1 for ADO.NET .

Install the EF7 Command Line Interface (CLI)

Before executing EF7 Core migrations or scripting commands, you must install the CLI package as a global or local tool.

To install the preview tools globally, install:

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:

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

You can use this new version of the EF7 CLI for projects that use an older version of the EF Core runtime.

daily builds

The EF7 preview is identical to the .NET 7 preview. These previews tend to lag behind the latest work on EF7. Consider using daily builds to get the latest EF7 features and bug fixes.
As with the preview, daily builds require .NET 6.

Documentation and Feedback

The entry point for all EF Core documentation is docs.microsoft.com/ef/.
Issues found and any other feedback are welcome on the dotnet/efcore GitHub repository .

useful url

The following links are provided for easy reference and access.

thanks from the team

The EF team is very grateful to everyone who has used and contributed to EF over the years! Welcome to EF7.


Long press to identify the QR code and follow Microsoft China MSDN

The complete plan for EF7


微软技术栈
423 声望996 粉丝

微软技术生态官方平台。予力众生,成就不凡!微软致力于用技术改变世界,助力企业实现数字化转型。