Skip to content

Commit d9fed7a

Browse files
CopilotAndriySvyryd
authored andcommitted
Document ToTable(null, buildAction) breaking change in EF Core 7.0 (#5338)
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
1 parent c1c7407 commit d9fed7a

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

entity-framework/core/what-is-new/ef-core-7.0/breaking-changes.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ EF Core 7.0 targets .NET 6. This means that existing applications that target .N
3333
| [Navigations from new entities to deleted entities are not fixed up](#deleted-fixup) | Low |
3434
| [Using `FromSqlRaw` and related methods from the wrong provider throws](#use-the-correct-method) | Low |
3535
| [Scaffolded `OnConfiguring` no longer calls `IsConfigured`](#is-configured) | Low |
36+
| [`ToTable(null, ...)` throws `ArgumentNullException`](#totable-null) | Low |
3637

3738
## High-impact changes
3839

@@ -573,3 +574,42 @@ Either:
573574

574575
- Use the `--no-onconfiguring` (.NET CLI) or `-NoOnConfiguring` (Visual Studio Package Manager Console) argument when scaffolding from an existing database.
575576
- [Customize the T4 templates](xref:core/managing-schemas/scaffolding/templates) to add back the call to `IsConfigured`.
577+
578+
<a name="totable-null"></a>
579+
580+
### `ToTable(null, ...)` throws `ArgumentNullException`
581+
582+
[Tracking Issue #19811](https://github.com/dotnet/efcore/issues/19811)
583+
584+
#### Old behavior
585+
586+
In EF Core 6.0, calling `.ToTable((string)null, t => t.ExcludeFromMigrations())` was a valid way to configure a keyless entity type that is not mapped to a table and excluded from migrations. For example:
587+
588+
```csharp
589+
modelBuilder.Entity<StringSplitResult>().HasNoKey().ToTable((string)null, t => t.ExcludeFromMigrations());
590+
```
591+
592+
#### New behavior
593+
594+
Starting with EF Core 7.0, calling `.ToTable((string)null, ...)` with the `Action<TableBuilder>` overload throws a `System.ArgumentNullException` because the `name` parameter is no longer nullable.
595+
596+
#### Why
597+
598+
This is a side effect of the changes made to support [table facets configuration](https://github.com/dotnet/efcore/issues/19811), which restructured the `ToTable` overloads.
599+
600+
#### Mitigations
601+
602+
Split the two calls. First, configure the table facets using the table builder overload (without specifying a null name), and then unmap the entity type from the table:
603+
604+
```csharp
605+
modelBuilder.Entity<StringSplitResult>(
606+
entityBuilder =>
607+
{
608+
entityBuilder.HasNoKey();
609+
entityBuilder.ToTable(t => t.ExcludeFromMigrations());
610+
entityBuilder.ToTable((string?)null);
611+
});
612+
```
613+
614+
> [!NOTE]
615+
> Calling `.ToTable(t => t.ExcludeFromMigrations())` will map the entity type to its default table, so migrations will ignore it. However, querying the entity type will try to query the default table unless you also configure it with `ToView` or another mapping override. Calling `.ToTable((string?)null)` afterward unmaps it from the table.

0 commit comments

Comments
 (0)