internal static int Create() { using (AdventureWorks adventureWorks = new AdventureWorks()) { ProductCategory category = new ProductCategory() { Name = "Category" }; ProductSubcategory subcategory = new ProductSubcategory() { Name = "Subcategory" }; category.ProductSubcategories.Add(subcategory); adventureWorks.ProductCategories.InsertOnSubmit(category); Trace.WriteLine(category.ProductCategoryID); // 0. Trace.WriteLine(subcategory.ProductCategoryID); // null. Trace.WriteLine(subcategory.ProductSubcategoryID); // 0. adventureWorks.SubmitChanges(); Trace.WriteLine(category.ProductCategoryID); // 5. Trace.WriteLine(subcategory.ProductCategoryID); // 5. Trace.WriteLine(subcategory.ProductSubcategoryID); // 38. return(subcategory.ProductSubcategoryID); } }
internal static void DeleteWithAssociation() { Create(); // Insert ProductCategory "Category" and ProductSubcategory "Subcategory". using (AdventureWorks adventureWorks = new AdventureWorks()) { ProductCategory category = adventureWorks.ProductCategories .Single(entity => entity.Name == "Category"); ProductSubcategory subcategory = adventureWorks.ProductSubcategories .Single(entity => entity.Name == "Subcategory"); adventureWorks.ProductCategories.DeleteOnSubmit(category); adventureWorks.ProductSubcategories.DeleteOnSubmit(subcategory); adventureWorks.SubmitChanges(); } }
internal static void DeleteWithNoQuery(int subcategoryId) { ProductSubcategory subcategory = new ProductSubcategory() { ProductSubcategoryID = subcategoryId }; using (AdventureWorks adventureWorks = new AdventureWorks()) { adventureWorks.ProductSubcategories.Attach(subcategory, false); adventureWorks.ProductSubcategories.DeleteOnSubmit(subcategory); adventureWorks.SubmitChanges(); } }
internal static void Update() { using (AdventureWorks adventureWorks = new AdventureWorks()) { ProductCategory category = adventureWorks.ProductCategories.First(); ProductSubcategory subcategory = adventureWorks.ProductSubcategories .Single(entity => entity.Name == "Subcategory"); Trace.WriteLine(subcategory.Name); // Subcategory. Trace.WriteLine(subcategory.ProductCategoryID); // 5. subcategory.Name = "Update"; // Update property. subcategory.ProductCategory = category; // Update association. adventureWorks.SubmitChanges(); Trace.WriteLine(subcategory.Name); // Subcategory update. Trace.WriteLine(subcategory.ProductCategoryID); // 4. } }
internal static void Default() { using (AdventureWorks adventureWorks = new AdventureWorks()) { ProductCategory category = adventureWorks.ProductCategories.First(); Trace.WriteLine(category.Name); // Accessories. category.Name = "Update"; ProductSubcategory subcategory = adventureWorks.ProductSubcategories.First(); Trace.WriteLine(subcategory.ProductCategoryID); // 1. subcategory.ProductCategoryID = -1; try { adventureWorks.SubmitChanges(); } catch (SqlException exception) { Trace.WriteLine(exception); // 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.Refresh(RefreshMode.OverwriteCurrentValues, category, subcategory); Trace.WriteLine(category.Name); // Accessories. Trace.WriteLine(subcategory.ProductCategoryID); // 1. } } }