Skip to content

Commit df58e89

Browse files
authored
Update TPC sample with Identity column code (#3990)
1 parent 9abe33c commit df58e89

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,17 @@ For databases that support sequences, key values can be generated by using a sin
732732

733733
`AnimalSequence` is a database sequence created by EF Core. This strategy is used by default for TPC hierarchies when using the EF Core database provider for SQL Server. Database providers for other databases that support sequences should have a similar default. Other key generation strategies that use sequences, such as Hi-Lo patterns, may also be used with TPC.
734734

735-
SQLite does not support sequences, and hence integer key value generation is not supported when using SQLite with the TPC strategy. However, client-side generation or globally unique keys--for example, GUID keys--are supported on any database, including SQLite.
735+
While standard Identity columns will not work with TPC, it is possible to use Identity columns if each table is configured with an appropriate seed and increment such that the values generated for each table will never conflict. For example:
736+
737+
<!--
738+
modelBuilder.Entity<Cat>().ToTable("Cats", tb => tb.Property(e => e.Id).UseIdentityColumn(1, 4));
739+
modelBuilder.Entity<Dog>().ToTable("Dogs", tb => tb.Property(e => e.Id).UseIdentityColumn(2, 4));
740+
modelBuilder.Entity<FarmAnimal>().ToTable("FarmAnimals", tb => tb.Property(e => e.Id).UseIdentityColumn(3, 4));
741+
modelBuilder.Entity<Human>().ToTable("Humans", tb => tb.Property(e => e.Id).UseIdentityColumn(4, 4));
742+
-->
743+
[!code-csharp[UsingIdentity](../../../../samples/core/Miscellaneous/NewInEFCore7/TpcInheritanceSample.cs?name=UsingIdentity)]
744+
745+
SQLite does not support sequences or Identity seed/increment, and hence integer key value generation is not supported when using SQLite with the TPC strategy. However, client-side generation or globally unique keys--for example, GUID keys--are supported on any database, including SQLite.
736746

737747
### Foreign key constraints
738748

samples/core/Miscellaneous/NewInEFCore7/NewInEFCore7.csproj

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.0-rc.1.22415.10" />
13-
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.0-rc.1.22415.10" />
14-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0-rc.1.22415.10" />
15-
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.0-rc.1.22415.10" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.0-rc.2.22420.3" />
13+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.0-rc.2.22420.3" />
14+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0-rc.2.22420.3" />
15+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.0-rc.2.22420.3" />
1616
</ItemGroup>
1717

1818
<ItemGroup>
19+
<Using Include="System.ComponentModel.DataAnnotations" />
1920
<Using Include="System.ComponentModel.DataAnnotations.Schema" />
2021
<Using Include="System.Data.Common" />
2122
<Using Include="System.Linq.Expressions" />

samples/core/Miscellaneous/NewInEFCore7/Program.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ public static async Task Main()
88
await TpcInheritanceSample.Inheritance_with_TPT();
99
await TpcInheritanceSample.Inheritance_with_TPC();
1010
await TpcInheritanceSample.Inheritance_with_TPC_using_HiLo();
11-
12-
// Currently not working: see https://github.com/dotnet/efcore/issues/28195
13-
// await TpcInheritanceSample.Inheritance_with_TPC_using_Identity();
11+
await TpcInheritanceSample.Inheritance_with_TPC_using_Identity();
1412

1513
await ExecuteDeleteSample.ExecuteDelete();
1614
await ExecuteDeleteSample.ExecuteDeleteTpt();

samples/core/Miscellaneous/NewInEFCore7/TpcInheritanceSample.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -374,13 +374,15 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
374374
{
375375
modelBuilder.Entity<Animal>().UseTpcMappingStrategy();
376376

377-
// Currently not working: see https://github.com/dotnet/efcore/issues/28195
378-
modelBuilder.Entity<FarmAnimal>().Property(e => e.Id).ValueGeneratedOnAdd().UseIdentityColumn(1, 4);
379-
modelBuilder.Entity<Cat>().Property(e => e.Id).ValueGeneratedOnAdd().UseIdentityColumn(2, 4);
380-
modelBuilder.Entity<Dog>().Property(e => e.Id).ValueGeneratedOnAdd().UseIdentityColumn(3, 4);
381-
modelBuilder.Entity<Human>().Property(e => e.Id).ValueGeneratedOnAdd().UseIdentityColumn(4, 4);
377+
#region UsingIdentity
378+
modelBuilder.Entity<Cat>().ToTable("Cats", tb => tb.Property(e => e.Id).UseIdentityColumn(1, 4));
379+
modelBuilder.Entity<Dog>().ToTable("Dogs", tb => tb.Property(e => e.Id).UseIdentityColumn(2, 4));
380+
modelBuilder.Entity<FarmAnimal>().ToTable("FarmAnimals", tb => tb.Property(e => e.Id).UseIdentityColumn(3, 4));
381+
modelBuilder.Entity<Human>().ToTable("Humans", tb => tb.Property(e => e.Id).UseIdentityColumn(4, 4));
382+
#endregion
382383

383384
modelBuilder.Entity<Food>().UseTpcMappingStrategy();
385+
384386
base.OnModelCreating(modelBuilder);
385387
}
386388
}

0 commit comments

Comments
 (0)