Skip to content

Commit

Permalink
Sample for stored procedure mapping (#4007)
Browse files Browse the repository at this point in the history
Co-authored-by: Shay Rojansky <roji@roji.org>
  • Loading branch information
ajcvickers and roji committed Sep 18, 2022
1 parent dfcd712 commit 439d3fa
Show file tree
Hide file tree
Showing 10 changed files with 1,362 additions and 85 deletions.
673 changes: 673 additions & 0 deletions samples/core/Miscellaneous/NewInEFCore7/DocumentsContext.cs

Large diffs are not rendered by default.

Large diffs are not rendered by default.

22 changes: 10 additions & 12 deletions samples/core/Miscellaneous/NewInEFCore7/ExecuteDeleteSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,25 @@ public static class ExecuteDeleteSample
{
public static Task ExecuteDelete()
{
Console.WriteLine($">>>> Sample: {nameof(ExecuteDelete)}");
Console.WriteLine();

PrintSampleName();
return ExecuteDeleteTest<TphBlogsContext>();
}

public static Task ExecuteDeleteTpt()
{
Console.WriteLine($">>>> Sample: {nameof(ExecuteDelete)}");
Console.WriteLine();

PrintSampleName();
return ExecuteDeleteTest<TptBlogsContext>();
}

public static Task ExecuteDeleteTpc()
{
Console.WriteLine($">>>> Sample: {nameof(ExecuteDelete)}");
Console.WriteLine();

PrintSampleName();
return ExecuteDeleteTest<TpcBlogsContext>();
}

public static Task ExecuteDeleteSqlite()
{
Console.WriteLine($">>>> Sample: {nameof(ExecuteDelete)}");
Console.WriteLine();

PrintSampleName();
return ExecuteDeleteTest<TphSqliteBlogsContext>();
}

Expand Down Expand Up @@ -274,6 +266,12 @@ await context.Posts.Where(p => p.Author!.Posts.Count <= 1)
$"Authors after delete: {string.Join(", ", await context.Authors.AsNoTracking().Select(e => "'" + e.Name + "'").ToListAsync())}");
Console.WriteLine();
}

private static void PrintSampleName([CallerMemberName] string? methodName = null)
{
Console.WriteLine($">>>> Sample: {methodName}");
Console.WriteLine();
}
}

public class TphBlogsContext : BlogsContext
Expand Down
34 changes: 16 additions & 18 deletions samples/core/Miscellaneous/NewInEFCore7/ExecuteUpdateSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,25 @@ public static class ExecuteUpdateSample
{
public static Task ExecuteUpdate()
{
Console.WriteLine($">>>> Sample: {nameof(ExecuteUpdate)}");
Console.WriteLine();

PrintSampleName();
return ExecuteUpdateTest<TphBlogsContext>();
}

public static Task ExecuteUpdateTpt()
{
Console.WriteLine($">>>> Sample: {nameof(ExecuteUpdate)}");
Console.WriteLine();

PrintSampleName();
return ExecuteUpdateTest<TptBlogsContext>();
}

public static Task ExecuteUpdateTpc()
{
Console.WriteLine($">>>> Sample: {nameof(ExecuteUpdate)}");
Console.WriteLine();

PrintSampleName();
return ExecuteUpdateTest<TpcBlogsContext>();
}

public static Task ExecuteUpdateSqlite()
{
Console.WriteLine($">>>> Sample: {nameof(ExecuteUpdate)}");
Console.WriteLine();

PrintSampleName();
return ExecuteUpdateTest<TphSqliteBlogsContext>();
}

Expand Down Expand Up @@ -179,16 +171,22 @@ private static async Task ResetPostPublishedOnToDefault<TContext>()
$"Posts before update: {string.Join(", ", await context.Posts.AsNoTracking().Select(e => "'..." + e.Title.Substring(e.Title.Length - 12) + "' " + e.PublishedOn.Date).ToListAsync())}");
Console.WriteLine();

context.LoggingEnabled = true;
await context.Set<Post>()
.ExecuteUpdateAsync(
setPropertyCalls => setPropertyCalls
.SetProperty(post => post.PublishedOn, post => EF.Default<DateTime>()));
context.LoggingEnabled = false;
// context.LoggingEnabled = true;
// await context.Set<Post>()
// .ExecuteUpdateAsync(
// setPropertyCalls => setPropertyCalls
// .SetProperty(post => post.PublishedOn, post => EF.Default<DateTime>()));
// context.LoggingEnabled = false;

Console.WriteLine();
Console.WriteLine(
$"Posts after update: {string.Join(", ", await context.Posts.AsNoTracking().Select(e => "'..." + e.Title.Substring(e.Title.Length - 12) + "' " + e.PublishedOn.Date).ToListAsync())}");
Console.WriteLine();
}

private static void PrintSampleName([CallerMemberName] string? methodName = null)
{
Console.WriteLine($">>>> Sample: {methodName}");
Console.WriteLine();
}
}
14 changes: 8 additions & 6 deletions samples/core/Miscellaneous/NewInEFCore7/JsonColumnsSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@ public static class JsonColumnsSample
{
public static Task Json_columns_with_TPH()
{
Console.WriteLine($">>>> Sample: {nameof(Json_columns_with_TPH)}");
Console.WriteLine();

PrintSampleName();
return JsonColumnsTest<JsonBlogsContext>();
}

public static Task Json_columns_with_TPH_on_SQLite()
{
Console.WriteLine($">>>> Sample: {nameof(Json_columns_with_TPH_on_SQLite)}");
Console.WriteLine();

PrintSampleName();
return JsonColumnsTest<JsonBlogsContextSqlite>();
}

Expand Down Expand Up @@ -170,6 +166,12 @@ private static async Task JsonColumnsTest<TContext>()

await context.SaveChangesAsync();
}

private static void PrintSampleName([CallerMemberName] string? methodName = null)
{
Console.WriteLine($">>>> Sample: {methodName}");
Console.WriteLine();
}
}

public abstract class JsonBlogsContextBase : BlogsContext
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System.Net;
using System.Runtime.CompilerServices;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#pragma warning disable CS0169

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

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.0-rc.2.22429.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.0-rc.2.22429.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0-rc.2.22429.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite" Version="7.0.0-rc.2.22429.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.0-rc.2.22429.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.0-rc.2.22451.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.0-rc.2.22451.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0-rc.2.22451.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite" Version="7.0.0-rc.2.22451.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.0-rc.2.22451.10" />
</ItemGroup>

<ItemGroup>
Expand All @@ -22,6 +22,7 @@
<Using Include="System.Data.Common" />
<Using Include="System.Linq.Expressions" />
<Using Include="System.Reflection" />
<Using Include="System.Runtime.CompilerServices" />
<Using Include="Microsoft.EntityFrameworkCore" />
<Using Include="Microsoft.Extensions.Logging" />
<Using Include="Microsoft.Extensions.DependencyInjection" />
Expand Down
56 changes: 30 additions & 26 deletions samples/core/Miscellaneous/NewInEFCore7/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,36 @@ public class Program
{
public static async Task Main()
{
await TpcInheritanceSample.Inheritance_with_TPH();
await TpcInheritanceSample.Inheritance_with_TPT();
await TpcInheritanceSample.Inheritance_with_TPC();
await TpcInheritanceSample.Inheritance_with_TPC_using_HiLo();
await TpcInheritanceSample.Inheritance_with_TPC_using_Identity();
// await TpcInheritanceSample.Inheritance_with_TPH();
// await TpcInheritanceSample.Inheritance_with_TPT();
// await TpcInheritanceSample.Inheritance_with_TPC();
// await TpcInheritanceSample.Inheritance_with_TPC_using_HiLo();
// await TpcInheritanceSample.Inheritance_with_TPC_using_Identity();
//
// await ExecuteDeleteSample.ExecuteDelete();
// await ExecuteDeleteSample.ExecuteDeleteTpt();
// await ExecuteDeleteSample.ExecuteDeleteTpc();
// await ExecuteDeleteSample.ExecuteDeleteSqlite();
//
// await ExecuteUpdateSample.ExecuteUpdate();
// await ExecuteUpdateSample.ExecuteUpdateTpt();
// await ExecuteUpdateSample.ExecuteUpdateTpc();
// await ExecuteUpdateSample.ExecuteUpdateSqlite();
//
// await JsonColumnsSample.Json_columns_with_TPH();
//
// // Issue https://github.com/dotnet/efcore/issues/28816 (Json: add support for Sqlite provider)
// // await JsonColumnsSample.Json_columns_with_TPH_on_SQLite();
//
// await ModelBuildingConventionsSample.No_foreign_key_index_convention();
// await ModelBuildingConventionsSample.Discriminator_length_convention();
// await ModelBuildingConventionsSample.Max_string_length_convention();
// await ModelBuildingConventionsSample.Map_members_explicitly_by_attribute_convention();
// await ModelBuildingConventionsSample.Custom_model_validation_convention();
// await ModelBuildingConventionsSample.No_cascade_delete_convention();

await ExecuteDeleteSample.ExecuteDelete();
await ExecuteDeleteSample.ExecuteDeleteTpt();
await ExecuteDeleteSample.ExecuteDeleteTpc();
await ExecuteDeleteSample.ExecuteDeleteSqlite();

await ExecuteUpdateSample.ExecuteUpdate();
await ExecuteUpdateSample.ExecuteUpdateTpt();
await ExecuteUpdateSample.ExecuteUpdateTpc();
await ExecuteUpdateSample.ExecuteUpdateSqlite();

await JsonColumnsSample.Json_columns_with_TPH();

// Issue https://github.com/dotnet/efcore/issues/28816 (Json: add support for Sqlite provider)
// await JsonColumnsSample.Json_columns_with_TPH_on_SQLite();

await ModelBuildingConventionsSample.No_foreign_key_index_convention();
await ModelBuildingConventionsSample.Discriminator_length_convention();
await ModelBuildingConventionsSample.Max_string_length_convention();
await ModelBuildingConventionsSample.Map_members_explicitly_by_attribute_convention();
await ModelBuildingConventionsSample.Custom_model_validation_convention();
await ModelBuildingConventionsSample.No_cascade_delete_convention();
await StoredProcedureMappingSample.Insert_Update_and_Delete_using_stored_procedures_with_TPH();
await StoredProcedureMappingSample.Insert_Update_and_Delete_using_stored_procedures_with_TPT();
await StoredProcedureMappingSample.Insert_Update_and_Delete_using_stored_procedures_with_TPC();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
namespace NewInEfCore7;

public static class StoredProcedureMappingSample
{
public static Task Insert_Update_and_Delete_using_stored_procedures_with_TPH()
{
PrintSampleName();
return SprocMappingTest<TphDocumentsContext>();
}

public static Task Insert_Update_and_Delete_using_stored_procedures_with_TPT()
{
PrintSampleName();
return SprocMappingTest<TptDocumentsContext>();
}

public static Task Insert_Update_and_Delete_using_stored_procedures_with_TPC()
{
PrintSampleName();
return SprocMappingTest<TpcDocumentsContext>();
}

private static async Task SprocMappingTest<TContext>()
where TContext : DocumentsContext, new()
{
await using var context = new TContext();
await context.Database.EnsureDeletedAsync();
context.LoggingEnabled = true;

Console.WriteLine("Creating database tables...");
Console.WriteLine();

await context.Database.EnsureCreatedAsync();

Console.WriteLine();
Console.WriteLine("Creating stored procedures...");
Console.WriteLine();

await context.CreateStoredProcedures();

context.LoggingEnabled = true;

Console.WriteLine();
Console.WriteLine("Seeding the database...");
Console.WriteLine();

await context.Seed();
context.ChangeTracker.Clear();

Console.WriteLine();
Console.WriteLine("Loading data...");
Console.WriteLine();

await context.Documents
.Include(document => ((Book)document).Authors)
.Include(document => ((Magazine)document).Editor)
.LoadAsync();

Console.WriteLine();
Console.WriteLine("Updating data...");
Console.WriteLine();

context.RemoveRange(context.People.Local.Where(person => person.Contact.Address.City == "Chigley"));
context.RemoveRange(context.Magazines.Local.Where(magazine => magazine.Title.Contains("Amstrad")));
context.RemoveRange(context.Books.Local.Where(book => book.NumberOfPages < 200));

foreach (var magazine in context.Magazines.Local)
{
magazine.CoverPrice += 1.0m;
}

foreach (var book in context.Books.Local)
{
book.Title += " (New Edition!)";
}

foreach (var person in context.People.Local.Where(person => person.Contact.Address.Country == "UK"))
{
person.Name = "Dr. " + person.Name;
person.Contact.Phone = "+44 " + person.Contact.Phone!.Substring(1);
person.Contact.Address.Country = "United Kingdom";
}

await context.SaveChangesAsync();

Console.WriteLine();
Console.WriteLine("Optimistic concurrency test 1...");
Console.WriteLine();

try
{
await using var context2 = new TContext();
(await context2.Books.SingleAsync(book => book.Title.StartsWith("Test"))).Isbn = "Mod1";

context.Books.Local.Single(book => book.Title.StartsWith("Test", StringComparison.Ordinal)).Isbn = null;
await context.SaveChangesAsync();

await context2.SaveChangesAsync();

}
catch (DbUpdateConcurrencyException exception)
{
Console.WriteLine($"Caught expected: " + exception.Message);
}

Console.WriteLine();
Console.WriteLine("Optimistic concurrency test 2...");
Console.WriteLine();

try
{
await using var context2 = new TContext();
(await context2.People.SingleAsync(person => person.Name.StartsWith("Dr. Kent"))).Name += ": Legend!";

context.People.Local.Single(person => person.Name.StartsWith("Dr. Kent", StringComparison.Ordinal)).Name += ": Hero!";
await context.SaveChangesAsync();

await context2.SaveChangesAsync();

}
catch (DbUpdateConcurrencyException exception)
{
Console.WriteLine($"Caught expected: " + exception.Message);
}

Console.WriteLine();
}

private static void PrintSampleName([CallerMemberName] string? methodName = null)
{
Console.WriteLine($">>>> Sample: {methodName}");
Console.WriteLine();
}
}
Loading

0 comments on commit 439d3fa

Please sign in to comment.