public static void Changing_relationships_using_navigations_2() { Console.WriteLine($">>>> Sample: {nameof(Changing_relationships_using_navigations_2)}"); Console.WriteLine(); Helpers.RecreateCleanDatabase(); Helpers.PopulateDatabase(); using var context = new BlogsContext(); var dotNetBlog = context.Blogs.Include(e => e.Posts).Single(e => e.Name == ".NET Blog"); var vsBlog = context.Blogs.Include(e => e.Posts).Single(e => e.Name == "Visual Studio Blog"); Console.WriteLine(context.ChangeTracker.DebugView.LongView); #region Changing_relationships_using_navigations_2 var post = vsBlog.Posts.Single(e => e.Title.StartsWith("Disassembly improvements")); post.Blog = dotNetBlog; #endregion context.ChangeTracker.DetectChanges(); Console.WriteLine(context.ChangeTracker.DebugView.LongView); context.SaveChanges(); Console.WriteLine(); }
public static void DbContext_verses_DbSet_methods_1() { Console.WriteLine($">>>> Sample: {nameof(DbContext_verses_DbSet_methods_1)}"); Console.WriteLine(); Helpers.RecreateCleanDatabase(); Helpers.PopulateDatabase(); #region DbContext_verses_DbSet_methods_1 using var context = new BlogsContext(); var post = context.Posts.Single(e => e.Id == 3); var tag = context.Tags.Single(e => e.Id == 1); var joinEntitySet = context.Set <Dictionary <string, int> >("PostTag"); var joinEntity = new Dictionary <string, int> { ["PostId"] = post.Id, ["TagId"] = tag.Id }; joinEntitySet.Add(joinEntity); Console.WriteLine(context.ChangeTracker.DebugView.LongView); context.SaveChanges(); #endregion Console.WriteLine(); }
public static void Deleting_an_entity_1() { Console.WriteLine($">>>> Sample: {nameof(Deleting_an_entity_1)}"); Console.WriteLine(); Helpers.RecreateCleanDatabase(); Helpers.PopulateDatabase(); #region Deleting_an_entity_1 using var context = new BlogsContext(); var vsBlog = context.Blogs .Include(e => e.Posts) .Include(e => e.Assets) .Single(e => e.Name == "Visual Studio Blog"); context.Remove(vsBlog); Console.WriteLine(context.ChangeTracker.DebugView.LongView); context.SaveChanges(); #endregion Console.WriteLine(); }
public static void Fixup_for_added_or_deleted_entities_3() { Console.WriteLine($">>>> Sample: {nameof(Fixup_for_added_or_deleted_entities_3)}"); Console.WriteLine(); Helpers.RecreateCleanDatabase(); Helpers.PopulateDatabase(); using var context = new BlogsContext(); var dotNetBlog = context.Blogs.Include(e => e.Posts).Single(e => e.Name == ".NET Blog"); Console.WriteLine(context.ChangeTracker.DebugView.LongView); #region Fixup_for_added_or_deleted_entities_3 var post = dotNetBlog.Posts.Single(e => e.Title == "Announcing F# 5"); dotNetBlog.Posts.Remove(post); #endregion context.ChangeTracker.DetectChanges(); Console.WriteLine(context.ChangeTracker.DebugView.LongView); context.SaveChanges(); Console.WriteLine(); }
public static void Fixup_for_added_or_deleted_entities_2() { Console.WriteLine($">>>> Sample: {nameof(Fixup_for_added_or_deleted_entities_2)}"); Console.WriteLine(); Helpers.RecreateCleanDatabase(); Helpers.PopulateDatabase(); using var context = new BlogsContext(); var dotNetBlog = context.Blogs.Include(e => e.Posts).Single(e => e.Name == ".NET Blog"); var vsBlog = context.Blogs.Include(e => e.Posts).Single(e => e.Name == "Visual Studio Blog"); Console.WriteLine(context.ChangeTracker.DebugView.LongView); #region Fixup_for_added_or_deleted_entities_2 var post = vsBlog.Posts.Single(e => e.Title.StartsWith("Disassembly improvements")); dotNetBlog.Posts.Add(post); #endregion context.ChangeTracker.DetectChanges(); Console.WriteLine(context.ChangeTracker.DebugView.LongView); context.SaveChanges(); Console.WriteLine(); }
public static void Temporary_values_2() { Console.WriteLine($">>>> Sample: {nameof(Temporary_values_2)}"); Console.WriteLine(); Helpers.RecreateCleanDatabase(); #region Temporary_values_2 var blogs = new List <Blog> { new Blog { Id = -1, Name = ".NET Blog" }, new Blog { Id = -2, Name = "Visual Studio Blog" } }; var posts = new List <Post> { new Post { Id = -1, BlogId = -1, Title = "Announcing the Release of EF Core 5.0", Content = "Announcing the release of EF Core 5.0, a full featured cross-platform..." }, new Post { Id = -2, BlogId = -2, Title = "Disassembly improvements for optimized managed debugging", Content = "If you are focused on squeezing out the last bits of performance for your .NET service or..." } }; using var context = new BlogsContext(); foreach (var blog in blogs) { context.Add(blog).Property(e => e.Id).IsTemporary = true; } foreach (var post in posts) { context.Add(post).Property(e => e.Id).IsTemporary = true; } Console.WriteLine(context.ChangeTracker.DebugView.LongView); context.SaveChanges(); Console.WriteLine(context.ChangeTracker.DebugView.LongView); #endregion Console.WriteLine(); }
public static void PopulateDatabase() { using var context = new BlogsContext(quiet: true); context.AddRange( new Blog { Name = ".NET Blog", Assets = new BlogAssets(), Posts = { new Post { Title = "Announcing the Release of EF Core 5.0", Content = "Announcing the release of EF Core 5.0, a full featured cross-platform..." }, new Post { Title = "Announcing F# 5", Content = "F# 5 is the latest version of F#, the functional programming language..." }, }, }, new Blog { Name = "Visual Studio Blog", Assets = new BlogAssets(), Posts = { new Post { Title = "Disassembly improvements for optimized managed debugging", Content = "If you are focused on squeezing out the last bits of performance for your .NET service or..." }, new Post { Title = "Database Profiling with Visual Studio", Content = "Examine when database queries were executed and measure how long the take using..." }, } }, new Tag { Text = ".NET" }, new Tag { Text = "Visual Studio" }, new Tag { Text = "EF Core" }); context.SaveChanges(); }
public static void Fixup_for_added_or_deleted_entities_7() { Console.WriteLine($">>>> Sample: {nameof(Fixup_for_added_or_deleted_entities_7)}"); Console.WriteLine(); Helpers.RecreateCleanDatabase(); Helpers.PopulateDatabase(); #region Fixup_for_added_or_deleted_entities_7 using var context = new BlogsContext(); var dotNetBlog = context.Blogs.Include(e => e.Assets).Single(e => e.Name == ".NET Blog"); dotNetBlog.Assets = new BlogAssets(); context.ChangeTracker.DetectChanges(); Console.WriteLine(context.ChangeTracker.DebugView.LongView); context.SaveChanges(); #endregion Console.WriteLine(); }