Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update TPC sample with Identity column code #3990

Merged
merged 1 commit into from
Aug 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion entity-framework/core/what-is-new/ef-core-7.0/whatsnew.md
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,17 @@ For databases that support sequences, key values can be generated by using a sin

`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.

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.
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:

<!--
modelBuilder.Entity<Cat>().ToTable("Cats", tb => tb.Property(e => e.Id).UseIdentityColumn(1, 4));
modelBuilder.Entity<Dog>().ToTable("Dogs", tb => tb.Property(e => e.Id).UseIdentityColumn(2, 4));
modelBuilder.Entity<FarmAnimal>().ToTable("FarmAnimals", tb => tb.Property(e => e.Id).UseIdentityColumn(3, 4));
modelBuilder.Entity<Human>().ToTable("Humans", tb => tb.Property(e => e.Id).UseIdentityColumn(4, 4));
-->
[!code-csharp[UsingIdentity](../../../../samples/core/Miscellaneous/NewInEFCore7/TpcInheritanceSample.cs?name=UsingIdentity)]

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.

### Foreign key constraints

Expand Down
9 changes: 5 additions & 4 deletions samples/core/Miscellaneous/NewInEFCore7/NewInEFCore7.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.0-rc.1.22415.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.0-rc.1.22415.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0-rc.1.22415.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.0-rc.1.22415.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.0-rc.2.22420.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.0-rc.2.22420.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0-rc.2.22420.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.0-rc.2.22420.3" />
</ItemGroup>

<ItemGroup>
<Using Include="System.ComponentModel.DataAnnotations" />
<Using Include="System.ComponentModel.DataAnnotations.Schema" />
<Using Include="System.Data.Common" />
<Using Include="System.Linq.Expressions" />
Expand Down
4 changes: 1 addition & 3 deletions samples/core/Miscellaneous/NewInEFCore7/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ public static async Task Main()
await TpcInheritanceSample.Inheritance_with_TPT();
await TpcInheritanceSample.Inheritance_with_TPC();
await TpcInheritanceSample.Inheritance_with_TPC_using_HiLo();

// Currently not working: see https://github.com/dotnet/efcore/issues/28195
// await TpcInheritanceSample.Inheritance_with_TPC_using_Identity();
await TpcInheritanceSample.Inheritance_with_TPC_using_Identity();

await ExecuteDeleteSample.ExecuteDelete();
await ExecuteDeleteSample.ExecuteDeleteTpt();
Expand Down
12 changes: 7 additions & 5 deletions samples/core/Miscellaneous/NewInEFCore7/TpcInheritanceSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,13 +374,15 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Animal>().UseTpcMappingStrategy();

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

modelBuilder.Entity<Food>().UseTpcMappingStrategy();

base.OnModelCreating(modelBuilder);
}
}
Expand Down