Example #1
0
        public async Task OnceExecuteBeginTransactionTest()
        {
            var stepId = Guid.NewGuid();
            var user   = new User {
                Name = "OnceExecuteBeginTransactionTest", Years = 18
            };

            await using (var testDbContext = new TestDbContext(DbName))
            {
                var ex = new OnceExecutorEf <TestDbContext, User>(testDbContext);

                // transaction 1
                var result = await ex.Execute(stepId,
                                              (context, c) => context.Users.AddAsync(user, c).AsTask(),
                                              (context, c) => context.Users.FirstOrDefaultAsync(x => x.Name == "OnceExecuteBeginTransactionTest", cancellationToken: c)
                                              );

                Assert.IsNotNull(result);
                Assert.AreEqual(user.Id, result.Id);

                await testDbContext.Users.AddAsync(new User()
                {
                    Name = "OnceExecuteBeginTransactionTest-2"
                });

                // transaction 2
                await testDbContext.SaveChangesAsync();
            }

            await CheckUsers("OnceExecuteBeginTransactionTest", "OnceExecuteBeginTransactionTest-2");
        }
Example #2
0
        public void DoubleInsertTest()
        {
            Assert.CatchAsync <DbUpdateException>(async() =>
            {
                var user = new User {
                    Name = "DoubleInsertTest", Years = 18
                };
                await using (var testDbContext = new TestDbContext(DbName))
                {
                    await testDbContext.Users.AddAsync(user);
                    await testDbContext.SaveChangesAsync();
                }

                await using (var testDbContext = new TestDbContext(DbName))
                {
                    await testDbContext.Users.AddAsync(user);
                    await testDbContext.SaveChangesAsync();
                }
            });
        }