internal static void ExplicitLoadingWithQuery(AdventureWorks adventureWorks)
        {
            ProductSubcategory subcategory = adventureWorks.ProductSubcategories.First(); // Execute query.

            // SELECT TOP(1) [p].[ProductSubcategoryID], [p].[Name], [p].[ProductCategoryID]
            // FROM [Production].[ProductSubcategory] AS [p]
            subcategory.Name.WriteLine();
            string categoryName = adventureWorks
                                  .Entry(subcategory).Reference(entity => entity.ProductCategory)
                                  .Query()                                     // Return IQueryable<ProductCategory>.
                                  .Select(category => category.Name).Single(); // Execute query.

            // exec sp_executesql N'SELECT TOP(2) [e].[Name]
            // FROM [Production].[ProductCategory] AS [e]
            // WHERE [e].[ProductCategoryID] = @__get_Item_0',N'@__get_Item_0 int',@__get_Item_0=1
            categoryName.WriteLine();

            IQueryable <string> products = adventureWorks
                                           .Entry(subcategory).Collection(entity => entity.Products)
                                           .Query()                          // Return IQueryable<Product>.
                                           .Select(product => product.Name); // Execute query.

            // exec sp_executesql N'SELECT [e].[Name]
            // FROM [Production].[Product] AS [e]
            // WHERE [e].[ProductSubcategoryID] = @__get_Item_0',N'@__get_Item_0 int',@__get_Item_0=1
            products.WriteLines();
        }
        internal static void ExplicitLoading(AdventureWorks adventureWorks)
        {
            ProductSubcategory subcategory = adventureWorks.ProductSubcategories.First(); // Execute query.

            // SELECT TOP(1) [p].[ProductSubcategoryID], [p].[Name], [p].[ProductCategoryID]
            // FROM [Production].[ProductSubcategory] AS [p]
            subcategory.Name.WriteLine();

            adventureWorks
            .Entry(subcategory)                          // Return EntityEntry<ProductSubcategory>.
            .Reference(entity => entity.ProductCategory) // Return ReferenceEntry<ProductSubcategory, ProductCategory>.
            .Load();                                     // Execute query.
            // exec sp_executesql N'SELECT [e].[ProductCategoryID], [e].[Name]
            // FROM [Production].[ProductCategory] AS [e]
            // WHERE [e].[ProductCategoryID] = @__get_Item_0',N'@__get_Item_0 int',@__get_Item_0=1
            subcategory.ProductCategory.Name.WriteLine();

            adventureWorks
            .Entry(subcategory)                    // Return EntityEntry<ProductSubcategory>.
            .Collection(entity => entity.Products) // Return CollectionEntry<ProductSubcategory, Product>.
            .Load();                               // Execute query.
            // exec sp_executesql N'SELECT [e].[ProductID], [e].[ListPrice], [e].[Name], [e].[ProductSubcategoryID]
            // FROM [Production].[Product] AS [e]
            // WHERE [e].[ProductSubcategoryID] = @__get_Item_0',N'@__get_Item_0 int',@__get_Item_0=1
            subcategory.Products.WriteLines(product => product.Name);
        }
Esempio n. 3
0
        internal static void Default(AdventureWorks adventureWorks)
        {
            ProductCategory category = adventureWorks.ProductCategories.First();

            category.Name = "Update"; // Valid value.g
            ProductSubcategory subcategory = adventureWorks.ProductSubcategories.First();

            subcategory.ProductCategoryID = -1; // Invalid value.
            try
            {
                adventureWorks.SaveChanges();
            }
            catch (DbUpdateException exception)
            {
                exception.WriteLine();
                // Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
                // ---> System.Data.SqlClient.SqlException: The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_ProductSubcategory_ProductCategory_ProductCategoryID". The conflict occurred in database "AdventureWorks", table "Production.ProductCategory", column 'ProductCategoryID'. The statement has been terminated.
                adventureWorks.Entry(category).Reload();
                category.Name.WriteLine();                 // Accessories
                adventureWorks.Entry(subcategory).Reload();
                subcategory.ProductCategoryID.WriteLine(); // 1
            }
        }