From dcdf03ca8e0ec034d98f5d4358403e7576dcee3b Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Thu, 10 Aug 2023 16:40:01 +0800 Subject: [PATCH] x --- IRaCIS.Core.API/Test.cs | 99 ----------------------------------------- 1 file changed, 99 deletions(-) delete mode 100644 IRaCIS.Core.API/Test.cs diff --git a/IRaCIS.Core.API/Test.cs b/IRaCIS.Core.API/Test.cs deleted file mode 100644 index f7567c3ca..000000000 --- a/IRaCIS.Core.API/Test.cs +++ /dev/null @@ -1,99 +0,0 @@ -using System; -using System.ComponentModel.DataAnnotations; -using System.Linq; -using Microsoft.EntityFrameworkCore; - -namespace EFSaving.Concurrency -{ - public class Sample - { - public static void Run() - { - // Ensure database is created and has a person in it - using (var setupContext = new PersonContext()) - { - setupContext.Database.EnsureDeleted(); - setupContext.Database.EnsureCreated(); - - setupContext.People.Add(new Person { FirstName = "John", LastName = "Doe" }); - setupContext.SaveChanges(); - } - - #region ConcurrencyHandlingCode - using var context = new PersonContext(); - // Fetch a person from database and change phone number - var person = context.People.Single(p => p.PersonId == 1); - person.PhoneNumber = "555-555-5555"; - - // Change the person's name in the database to simulate a concurrency conflict - context.Database.ExecuteSqlRaw( - "UPDATE dbo.People SET FirstName = 'Jane' WHERE PersonId = 1"); - - var saved = false; - while (!saved) - { - try - { - // Attempt to save changes to the database - context.SaveChanges(); - saved = true; - } - catch (DbUpdateConcurrencyException ex) - { - foreach (var entry in ex.Entries) - { - if (entry.Entity is Person) - { - var proposedValues = entry.CurrentValues; - var databaseValues = entry.GetDatabaseValues(); - - foreach (var property in proposedValues.Properties) - { - var proposedValue = proposedValues[property]; - var databaseValue = databaseValues[property]; - - // TODO: decide which value should be written to database - // proposedValues[property] = ; - } - - // Refresh original values to bypass next concurrency check - entry.OriginalValues.SetValues(databaseValues); - } - else - { - throw new NotSupportedException( - "Don't know how to handle concurrency conflicts for " - + entry.Metadata.Name); - } - } - } - } - #endregion - } - - public class PersonContext : DbContext - { - public DbSet People { get; set; } - - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - // Requires NuGet package Microsoft.EntityFrameworkCore.SqlServer - optionsBuilder.UseSqlServer( - @"Server=(localdb)\mssqllocaldb;Database=EFSaving.Concurrency;Trusted_Connection=True"); - } - } - - public class Person - { - public int PersonId { get; set; } - - [ConcurrencyCheck] - public string FirstName { get; set; } - - [ConcurrencyCheck] - public string LastName { get; set; } - - public string PhoneNumber { get; set; } - } - } -} \ No newline at end of file