示例#1
0
        public static void Run()
        {
            MyDbContext.EnsureDatabaseIsCleaned();

            InitDatabase();

            Console.WriteLine("Step 0: No query yet (in a fresh DB Context).");
            Step0();
            Console.WriteLine();

            Console.WriteLine("Step 1: make a NoTracking Query");
            AsNoTrackingQuery();
            Console.WriteLine();

            Console.WriteLine("Step 2: make a Tracking Query with AsQueryable");
            AsQueryableQuery();
            Console.WriteLine();

            Console.WriteLine("Step 3: make another NoTracking Query");
            AsNoTrackingQuery();
            Console.WriteLine();

            Console.WriteLine("Step 4: make a Tracking Query with AsEnumerable");
            AsEnumerableQuery();
            Console.WriteLine();
        }
示例#2
0
        private static async Task Main()
        {
            MyDbContext.EnsureDatabaseIsCleaned();
            using (var dbContext = new MyDbContext())
            {
                dbContext.Database.Migrate();
                if (dbContext.Database.IsSqlite())
                {
                    await dbContext.Database.ExecuteSqlRawAsync(
                        @"
                            CREATE TRIGGER SetTimestampOnUpdate
                            AFTER UPDATE ON ConcurrentAccountsWithRowVersion
                            BEGIN
                                UPDATE ConcurrentAccountsWithRowVersion
                                SET Timestamp = randomblob(8)
                                WHERE rowid = NEW.rowid;
                            END
                        ");

                    await dbContext.Database.ExecuteSqlRawAsync(
                        @"
                            CREATE TRIGGER SetTimestampOnInsert
                            AFTER INSERT ON ConcurrentAccountsWithRowVersion
                            BEGIN
                                UPDATE ConcurrentAccountsWithRowVersion
                                SET Timestamp = randomblob(8)
                                WHERE rowid = NEW.rowid;
                            END
                        ");
                }
                await dbContext.NonconcurrentAccounts.AddAsync(new NonconcurrentAccount { Id = 1, Balance = 1000.0m });

                await dbContext.ConcurrentAccountsWithToken.AddAsync(new ConcurrentAccountWithToken { Id = 1, Balance = 1000.0m });

                await dbContext.ConcurrentAccountsWithRowVersion.AddAsync(new ConcurrentAccountWithRowVersion { Id = 1, Balance = 1000.0m });

                await dbContext.SaveChangesAsync();
            }

            Console.WriteLine("========== Concurrency Test with NonConcurrent Account ==============================");
            await TestWithoutConcurrencyControl();

            Console.WriteLine("\n\n========== Concurrency Test with Concurrent Account using Concurrent Token ==========");
            await ConcurrencyControlByConcurrencyToken();

            Console.WriteLine("\n\n========== Concurrency Test with Concurrent Account using Row Version ===============");
            await ConcurrencyControlByRowVersion();
        }