public static void Main() { #region RegisterDiagnosticListener DiagnosticListener.AllListeners.Subscribe(new DiagnosticObserver()); #endregion using (var context = new BlogsContext()) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); context.Add( new Blog { Name = "EF Blog", Posts = { new Post { Title = "EF Core 3.1!" }, new Post { Title = "EF Core 5.0!" } } }); context.SaveChanges(); } using (var context = new BlogsContext()) { var blog = context.Blogs.Include(e => e.Posts).Single(); blog.Name = "EF Core Blog"; context.Remove(blog.Posts.First()); blog.Posts.Add(new Post { Title = "EF Core 6.0!" }); context.SaveChanges(); } #endregion }
public static void Main() { #region Demonstration using (var context = new BlogsContext()) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); context.Add( new Blog { Id = 1, Name = "EF Blog", Posts = { new Post { Id = 1, Title = "EF Core 3.1!" }, new Post { Id = 2, Title = "EF Core 5.0!" } } }); context.SaveChanges(); } using (var context = new BlogsContext()) { var blog = context.Blogs.Include(e => e.Posts).Single(); blog.Name = "EF Core Blog"; context.Remove(blog.Posts.First()); blog.Posts.Add(new Post { Id = 3, Title = "EF Core 6.0!" }); context.SaveChanges(); } #endregion }
public static void Using_DbSet_Local_to_query_tracked_entities_2() { Console.WriteLine($">>>> Sample: {nameof(Using_DbSet_Local_to_query_tracked_entities_2)}"); Console.WriteLine(); Helpers.RecreateCleanDatabase(); Helpers.PopulateDatabase(); #region Using_DbSet_Local_to_query_tracked_entities_2 using var context = new BlogsContext(); var posts = context.Posts.Include(e => e.Blog).ToList(); Console.WriteLine("Local view after loading posts:"); foreach (var post in context.Posts.Local) { Console.WriteLine($" Post: {post.Title}"); } context.Remove(posts[1]); context.Add( new Post { Title = "What’s next for System.Text.Json?", Content = ".NET 5.0 was released recently and has come with many...", Blog = posts[0].Blog }); Console.WriteLine("Local view after adding and deleting posts:"); foreach (var post in context.Posts.Local) { Console.WriteLine($" Post: {post.Title}"); } #endregion Console.WriteLine(); }
public static async Task Main() { await CreateDatabases(); #region Program // Insert, update, and delete some entities using (var context = new BlogsContext()) { context.Add( new Blog { Name = "EF Blog", Posts = { new Post { Title = "EF Core 3.1!" }, new Post{ Title = "EF Core 5.0!" } } }); await context.SaveChangesAsync(); } using (var context = new BlogsContext()) { var blog = context.Blogs.Include(e => e.Posts).Single(); blog.Name = "EF Core Blog"; context.Remove(blog.Posts.First()); blog.Posts.Add(new Post { Title = "EF Core 6.0!" }); context.SaveChanges(); } // Do an insert that will fail using (var context = new BlogsContext()) { try { context.Add(new Post { Id = 3, Title = "EF Core 3.1!" }); await context.SaveChangesAsync(); } catch (DbUpdateException) { } } // Look at the audit trail using (var context = new AuditContext("DataSource=audit.db")) { foreach (var audit in context.SaveChangesAudits.Include(e => e.Entities).ToList()) { Console.WriteLine( $"Audit {audit.AuditId} from {audit.StartTime} to {audit.EndTime} was{(audit.Succeeded ? "" : " not")} successful."); foreach (var entity in audit.Entities) { Console.WriteLine($" {entity.AuditMessage}"); } if (!audit.Succeeded) { Console.WriteLine($" Error: {audit.ErrorMessage}"); } } } #endregion }