internal static void Default() { using (AdventureWorks adventureWorks = new 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) { Trace.WriteLine(exception); // System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. // ---> System.Data.Entity.Core.UpdateException: 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 "D:\ONEDRIVE\WORKS\DRAFTS\CODESNIPPETS\DATA\ADVENTUREWORKS_DATA.MDF", table "Production.ProductCategory", column 'ProductCategoryID'. The statement has been terminated. adventureWorks.Entry(category).Reload(); Trace.WriteLine(category.Name); // Accessories adventureWorks.Entry(subcategory).Reload(); Trace.WriteLine(subcategory.ProductCategoryID); // 1 } } }
internal static ProductCategory Create() { using (AdventureWorks adventureWorks = new AdventureWorks()) { ProductCategory category = new ProductCategory() { Name = nameof(ProductCategory) }; ProductSubcategory subcategory = new ProductSubcategory() { Name = nameof(ProductSubcategory) }; adventureWorks.ProductSubcategories.Add(subcategory); subcategory.ProductCategory = category; // Equivalent to: category.ProductSubcategories.Add(subcategory); Trace.WriteLine(adventureWorks.ChangeTracker.Entries() .Count(tracking => tracking.State == EntityState.Added)); // 2 Trace.WriteLine(category.ProductCategoryID); // 0 Trace.WriteLine(subcategory.ProductCategoryID); // 0 Trace.WriteLine(subcategory.ProductSubcategoryID); // 0 Trace.WriteLine(adventureWorks.SaveChanges()); // 2 Trace.WriteLine(adventureWorks.ChangeTracker.Entries() .Count(tracking => tracking.State != EntityState.Unchanged)); // 0 Trace.WriteLine(category.ProductCategoryID); // 25 Trace.WriteLine(subcategory.ProductCategoryID); // 25 Trace.WriteLine(subcategory.ProductSubcategoryID); // 50 return(category); } }
internal static ProductCategory Create() { using (AdventureWorks adventureWorks = new AdventureWorks()) { ProductCategory category = new ProductCategory() { Name = "Create" }; ProductSubcategory subcategory = new ProductSubcategory() { Name = "Create" }; adventureWorks.ProductSubcategories.Add(subcategory); // Track creation. subcategory.ProductCategory = category; // Equivalent to: category.ProductSubcategories.Add(subcategory); adventureWorks.ChangeTracker.Entries() .Count(tracking => tracking.State == EntityState.Added).WriteLine(); // 2 object.ReferenceEquals(category.ProductSubcategories.Single(), subcategory).WriteLine(); // True. category.ProductCategoryID.WriteLine(); // 0 subcategory.ProductCategoryID.WriteLine(); // 0 subcategory.ProductSubcategoryID.WriteLine(); // 0 adventureWorks.SaveChanges().WriteLine(); // 2 // BEGIN TRANSACTION // exec sp_executesql N'SET NOCOUNT ON; // INSERT INTO [Production].[ProductCategory] ([Name]) // VALUES (@p0); // SELECT [ProductCategoryID] // FROM [Production].[ProductCategory] // WHERE @@ROWCOUNT = 1 AND [ProductCategoryID] = scope_identity(); // ',N'@p0 nvarchar(50)',@p0=N'Create' // exec sp_executesql N'SET NOCOUNT ON; // INSERT INTO [Production].[ProductCategory] ([Name]) // VALUES (@p0); // SELECT [ProductCategoryID] // FROM [Production].[ProductCategory] // WHERE @@ROWCOUNT = 1 AND [ProductCategoryID] = scope_identity(); // ',N'@p0 nvarchar(50)',@p0=N'Create' // COMMIT TRANSACTION adventureWorks.ChangeTracker.Entries() .Count(tracking => tracking.State != EntityState.Unchanged).WriteLine(); // 0 category.ProductCategoryID.WriteLine(); // 25 subcategory.ProductCategoryID.WriteLine(); // 25 subcategory.ProductSubcategoryID.WriteLine(); // 50 return(category); } // Unit of work. }