Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.71 KB | None | 0 0
  1. using System;
  2. using System.Threading.Tasks;
  3. using ConcurrencyTests.Api;
  4. using Microsoft.EntityFrameworkCore;
  5.  
  6. namespace ConcurrencyTestsConsole
  7. {
  8.     public class Parent
  9.     {
  10.         public int Id { get; set; }
  11.         public ICollection<Child> Children { get; set; }
  12.     }
  13.  
  14.     public class Child
  15.     {
  16.         public int Id { get; set; }
  17.         public string Name { get; set; }
  18.         public int ParentId { get; set; }
  19.         public Parent Parent { get; set; }
  20.     }
  21.  
  22.     public class AppDbContext : DbContext
  23.     {
  24.         public DbSet<Parent> Parents { get; set; }
  25.         public DbSet<Child> Children { get; set; }
  26.  
  27.         protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  28.         {
  29.             optionsBuilder.UseNpgsql("Host=localhost;Database=concurrency_tests;Username=postgres;Password=admin");
  30.         }
  31.     }
  32.  
  33.     class Program
  34.     {
  35.         static async Task CreateParent()
  36.         {
  37.             using (var db = new AppDbContext())
  38.             {
  39.                 db.Parents.Add(new Parent());
  40.                 await db.SaveChangesAsync();
  41.             }
  42.         }
  43.  
  44.         static async Task DeleteParents()
  45.         {
  46.             using (var db = new AppDbContext())
  47.             {
  48.                 await Task.Delay(1000);
  49.                 db.Parents.RemoveRange(db.Parents);
  50.                 await db.SaveChangesAsync();
  51.  
  52.                 Console.WriteLine("Parent was deleted");
  53.             }
  54.         }
  55.  
  56.         static async Task CreateChild()
  57.         {
  58.             using (var db = new AppDbContext())
  59.             {
  60.                 using (var transaction = db.Database.BeginTransaction())
  61.                 {
  62.                     var parent = await db.Parents.FirstOrDefaultAsync();
  63.                     if (parent == null)
  64.                     {
  65.                         Console.WriteLine("Parent cannot be null!");
  66.                         return;
  67.                     }
  68.  
  69.                     Console.WriteLine("Parent exists!");
  70.  
  71.                     await Task.Delay(3000);
  72.  
  73.                     db.Children.Add(new Child
  74.                     {
  75.                         Name = "dupa",
  76.                         Parent = parent
  77.                     });
  78.  
  79.                     Console.WriteLine("Trying to add a child with deleted parent");
  80.                     await db.SaveChangesAsync();
  81.  
  82.                     Console.WriteLine("Child added");
  83.                 }
  84.             }
  85.         }
  86.  
  87.         static async Task Main()
  88.         {
  89.             await CreateParent();
  90.  
  91.             var createChildTask = CreateChild();
  92.             var deleteParentsTask = DeleteParents();
  93.             await Task.WhenAll(createChildTask, deleteParentsTask);
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement