示例#1
0
        internal static async Task DbContextTransactionAsync(AdventureWorks adventureWorks)
        {
            await adventureWorks.Database.CreateExecutionStrategy().ExecuteAsync(async() =>
            {
                using (IDbContextTransaction transaction = await adventureWorks.Database.BeginTransactionAsync(
                           IsolationLevel.ReadUncommitted))
                {
                    try
                    {
                        adventureWorks.CurrentIsolationLevel().WriteLine(); // ReadUncommitted

                        ProductCategory category = new ProductCategory()
                        {
                            Name = nameof(ProductCategory)
                        };
                        await adventureWorks.ProductCategories.AddAsync(category);
                        (await adventureWorks.SaveChangesAsync()).WriteLine(); // 1

                        await adventureWorks.Database.ExecuteSqlCommandAsync(
                            sql: "DELETE FROM [Production].[ProductCategory] WHERE [Name] = {0}",
                            parameters: nameof(ProductCategory)).WriteLine(); // 1
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            });
        }
示例#2
0
        internal static async Task DbTransactionAsync()
        {
            using (SqlConnection connection = new SqlConnection(ConnectionStrings.AdventureWorks))
            {
                await connection.OpenAsync();

                using (DbTransaction transaction = connection.BeginTransaction(IsolationLevel.Serializable))
                {
                    try
                    {
                        using (AdventureWorks adventureWorks = new AdventureWorks(connection))
                        {
                            await adventureWorks.Database.CreateExecutionStrategy().ExecuteAsync(async() =>
                            {
                                adventureWorks.Database.UseTransaction(transaction);
                                adventureWorks.CurrentIsolationLevel().WriteLine(); // Serializable

                                ProductCategory category = new ProductCategory()
                                {
                                    Name = nameof(ProductCategory)
                                };
                                await adventureWorks.ProductCategories.AddAsync(category);
                                (await adventureWorks.SaveChangesAsync()).WriteLine(); // 1.
                            });
                        }

                        using (DbCommand command = connection.CreateCommand())
                        {
                            command.CommandText = "DELETE FROM [Production].[ProductCategory] WHERE [Name] = @p0";
                            DbParameter parameter = command.CreateParameter();
                            parameter.ParameterName = "@p0";
                            parameter.Value         = nameof(ProductCategory);
                            command.Parameters.Add(parameter);
                            command.Transaction = transaction;
                            (await command.ExecuteNonQueryAsync()).WriteLine(); // 1
                        }
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
示例#3
0
        internal static async Task Async(AdventureWorks adventureWorks)
        {
            IQueryable <ProductCategory> categories = adventureWorks.ProductCategories;
            await categories.ForEachAsync( // Async version of foreach/ForEach.
                category => category.Name.WriteLine());

            ProductSubcategory subcategory = await adventureWorks.ProductSubcategories
                                             .FirstAsync(entity => entity.Name.Contains("Bike")); // Async version of First.

            subcategory.Name.WriteLine();

            Product[] products = await adventureWorks.Products
                                 .Where(product => product.ListPrice <= 10)
                                 .ToArrayAsync(); // Async version of ToArray.

            adventureWorks.Products.RemoveRange(products);
            (await adventureWorks.SaveChangesAsync()).WriteLine(); // Async version of SaveChanges.
        }
示例#4
0
        internal static async Task SaveChangesAsync()
        {
            using (AdventureWorks adventureWorks1 = new AdventureWorks())
                using (AdventureWorks adventureWorks2 = new AdventureWorks())
                {
                    int     id           = 950;
                    Product productCopy1 = await adventureWorks1.Products.FindAsync(id);

                    Product productCopy2 = await adventureWorks2.Products.FindAsync(id);

                    productCopy1.Name      = nameof(productCopy1);
                    productCopy1.ListPrice = 100;
                    (await adventureWorks1.SaveChangesAsync()).WriteLine(); // 1

                    productCopy2.Name = nameof(productCopy2);
                    productCopy2.ProductSubcategoryID = 1;
                    (await adventureWorks2.SaveChangesAsync(RefreshConflict.MergeClientAndStore)).WriteLine(); // 1
                }
        }