-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sample for stored procedure mapping (#4007)
Co-authored-by: Shay Rojansky <roji@roji.org>
- Loading branch information
1 parent
dfcd712
commit 439d3fa
Showing
10 changed files
with
1,362 additions
and
85 deletions.
There are no files selected for viewing
673 changes: 673 additions & 0 deletions
673
samples/core/Miscellaneous/NewInEFCore7/DocumentsContext.cs
Large diffs are not rendered by default.
Oops, something went wrong.
474 changes: 474 additions & 0 deletions
474
samples/core/Miscellaneous/NewInEFCore7/DocumentsContextStoredProcedures.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 0 additions & 3 deletions
3
samples/core/Miscellaneous/NewInEFCore7/ModelBuildingConventionsSample.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
samples/core/Miscellaneous/NewInEFCore7/StoredProcedureMappingSample.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.