Skip to content

Commit

Permalink
Merge pull request #2531 from dotnet/master
Browse files Browse the repository at this point in the history
Publish what's new in 5.0 Preview7
  • Loading branch information
smitpatel committed Jul 21, 2020
2 parents d85263b + 99cee66 commit 5114892
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The main documentation on required and optional properties and their interaction
## DbContext and DbSet

When nullable reference types are enabled, the C# compiler emits warnings for any uninitialized non-nullable property, as these would contain null. As a result, the common practice of defining a non-nullable `DbSet` on a context will now generate a warning. However, EF Core always initializes all `DbSet` properties on DbContext-derived types, so they are guaranteed to never be null, even if the compiler is unaware of this. Therefore, it is recommended to keep your `DbSet` properties non-nullable - allowing you to access them without null checks - and to silence the compiler warnings by explicitly setting them to null with the help of the null-forgiving operator (!):
When nullable reference types are enabled, the C# compiler emits warnings for any uninitialized non-nullable property, as these would contain null. As a result, the common practice of having uninitialized DbSet properties on a context type will now generate a warning. To fix this, make your DbSet properties read-only and initialize them as follows:

[!code-csharp[Main](../../../samples/core/Miscellaneous/NullableReferenceTypes/NullableReferenceTypesContext.cs?name=Context&highlight=3-4)]

Expand Down
144 changes: 143 additions & 1 deletion entity-framework/core/what-is-new/ef-core-5.0/whatsnew.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: What's New in EF Core 5.0
description: Overview of new features in EF Core 5.0
author: ajcvickers
ms.date: 06/02/2020
ms.date: 07/20/2020
uid: core/what-is-new/ef-core-5.0/whatsnew
---

Expand All @@ -14,6 +14,148 @@ This page does not duplicate the [plan for EF Core 5.0](xref:core/what-is-new/ef

We will add links from here to the official documentation as it is published.

## Preview 7

### DbContextFactory

EF Core 5.0 introduces `AddDbContextFactory` and `AddPooledDbContextFactory` to register a factory for creating DbContext instances in the application's dependency injection (D.I.) container. For example:

```csharp
services.AddDbContextFactory<SomeDbContext>(b =>
b.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test"));
```

Application services such as ASP.NET Core controllers can then depend on `IDbContextFactory<TContext>` in the service constructor. For example:

```csharp
public class MyController
{
private readonly IDbContextFactory<SomeDbContext> _contextFactory;

public MyController(IDbContextFactory<SomeDbContext> contextFactory)
{
_contextFactory = contextFactory;
}
}
```

DbContext instances can then be created and used as needed. For example:

```csharp
public void DoSomehing()
{
using (var context = _contextFactory.CreateDbContext())
{
// ...
}
}
```

Note that the DbContext instances created in this way are _not_ managed by the application's service provider and therefore must be disposed by the application. This decoupling is very useful for Blazor applications, where using `IDbContextFactory` is recommended, but may also be useful in other scenarios.

DbContext instances can be pooled by calling `AddPooledDbContextFactory`. This pooling works the same way as for `AddDbContextPool`, and also has the same limitations.

Documentation is tracked by issue [#2523](https://github.com/dotnet/EntityFramework.Docs/issues/2523).

### Reset DbContext state

EF Core 5.0 introduces `ChangeTracker.Clear()` which clears the DbContext of all tracked entities. This should usually not be needed when using the best practice of creating a new, short-lived context instance for each unit-of-work. However, if there is a need to reset the state of a DbContext instance, then using the new `Clear()` method is more performant and robust than mass-detaching all entities.

Documentation is tracked by issue [#2524](https://github.com/dotnet/EntityFramework.Docs/issues/2524).

### New pattern for store-generated defaults

EF Core allows an explicit value to be set for a column that may also have default value constraint. EF Core uses the CLR default of type property type as a sentinel for this; if the value is not the CLR default, then it is inserted, otherwise the database default is used.

This creates problems for types where the CLR default is not a good sentinel--most notably, `bool` properties. EF Core 5.0 now allows the backing field to be nullable for cases like this. For example:

```csharp
public class Blog
{
private bool? _isValid;

public bool IsValid
{
get => _isValid ?? false;
set => _isValid = value;
}
}
```

Note that the backing field is nullable, but the publicly exposed property is not. This allows the sentinel value to be `null` without impacting the public surface of the entity type. In this case, if the `IsValid` is never set, then the database default will be used since the backing field remains null. If either `true` or `false` are set, then this value is saved explicitly to the database.

Documentation is tracked by issue [#2525](https://github.com/dotnet/EntityFramework.Docs/issues/2525).

### Cosmos partition keys

EF Core allows the Cosmos partition key is included in the EF model. For example:

```csharp
modelBuilder.Entity<Customer>().HasPartitionKey(b => b.AlternateKey)
```

Starting with preview 7, the partition key is included in the entity type's PK and is used to improved performance in some queries.

Documentation is tracked by issue [#2471](https://github.com/dotnet/EntityFramework.Docs/issues/2471).

### Cosmos configuration

EF Core 5.0 improves configuration of Cosmos and Cosmos connections.

Previously, EF Core required the end-point and key to be specified explicitly when connecting to a Cosmos database. EF Core 5.0 allows use of a connection string instead. In addition, EF Core 5.0 allows the WebProxy instance to be explicitly set. For example:

```csharp
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseCosmos("my-cosmos-connection-string", "MyDb",
cosmosOptionsBuilder =>
{
cosmosOptionsBuilder.WebProxy(myProxyInstance);
});
```

Many other timeout values, limits, etc. can now also be configured. For example:

```csharp
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseCosmos("my-cosmos-connection-string", "MyDb",
cosmosOptionsBuilder =>
{
cosmosOptionsBuilder.LimitToEndpoint();
cosmosOptionsBuilder.RequestTimeout(requestTimeout);
cosmosOptionsBuilder.OpenTcpConnectionTimeout(timeout);
cosmosOptionsBuilder.IdleTcpConnectionTimeout(timeout);
cosmosOptionsBuilder.GatewayModeMaxConnectionLimit(connectionLimit);
cosmosOptionsBuilder.MaxTcpConnectionsPerEndpoint(connectionLimit);
cosmosOptionsBuilder.MaxRequestsPerTcpConnection(requestLimit);
});
```

Finally, the default connection mode is now `ConnectionMode.Gateway`, which is generally more compatible.

Documentation is tracked by issue [#2471](https://github.com/dotnet/EntityFramework.Docs/issues/2471).

### Scaffold-DbContext now singularizes

Previously when scaffolding a DbContext from an existing database, EF Core will create entity type names that match the table names in the database. For example, tables `People` and `Addresses` resulted in entity types named `People` and `Addresses`.

In previous releases, this behavior was configurable through registration of a pluralization service. Now in EF Core 5.0, the [Humanizer](https://www.nuget.org/packages/Humanizer.Core/) package is used as a default pluralization service. This means tables `People` and `Addresses` will now be reverse engineered to entity types named `Person` and `Address`.

### Savepoints

EF Core now supports [savepoints](/SQL/t-sql/language-elements/save-transaction-transact-sql?view=sql-server-ver15#remarks) for greater control over transactions that execute multiple operations.

Savepoints can be manually created, released, and rolled back. For example:

```csharp
context.Database.CreateSavepoint("MySavePoint");
```

In addition, EF Core will now roll back to the last savepoint when executing `SaveChanges` fails. This allows SaveChanges to be re-tried without re-trying the entire transaction.

Documentation is tracked by issue [#2429](https://github.com/dotnet/EntityFramework.Docs/issues/2429).

## Preview 6

### Split queries for related collections
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace NullableReferenceTypes
#region Context
public class NullableReferenceTypesContext : DbContext
{
public DbSet<Customer> Customers { get; set; } = null!;
public DbSet<Order> Orders { get; set; } = null!;
public DbSet<Customer> Customers => Set<Customer>();
public DbSet<Order> Orders => Set<Order>();

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
Expand Down

0 comments on commit 5114892

Please sign in to comment.