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 Work_with_a_single_navigation_2() { Console.WriteLine($">>>> Sample: {nameof(Work_with_a_single_navigation_2)}"); Console.WriteLine(); Helpers.RecreateCleanDatabase(); Helpers.PopulateDatabase(); using var context = new BlogsContext(); var blog = context.Blogs.Include(e => e.Posts).Single(e => e.Id == 1); #region Work_with_a_single_navigation_2a CollectionEntry <Blog, Post> collectionEntry1 = context.Entry(blog).Collection(e => e.Posts); CollectionEntry <Blog, Post> collectionEntry2 = context.Entry(blog).Collection <Post>("Posts"); CollectionEntry collectionEntry3 = context.Entry(blog).Collection("Posts"); #endregion #region Work_with_a_single_navigation_2b NavigationEntry navigationEntry = context.Entry(blog).Navigation("Posts"); #endregion Console.WriteLine(); }
public static void Work_with_all_properties_of_an_entity_1() { Console.WriteLine($">>>> Sample: {nameof(Work_with_all_properties_of_an_entity_1)}"); Console.WriteLine(); Helpers.RecreateCleanDatabase(); Helpers.PopulateDatabase(); using var context = new BlogsContext(); var blog = context.Blogs.Include(e => e.Posts).Single(e => e.Id == 1); #region Work_with_all_properties_of_an_entity_1 foreach (var propertyEntry in context.Entry(blog).Properties) { if (propertyEntry.Metadata.ClrType == typeof(DateTime)) { propertyEntry.CurrentValue = DateTime.Now; } } #endregion Console.WriteLine(); }
public static async Task Main() { using (var context = new BlogsContext()) { await context.Database.EnsureDeletedAsync(); await context.Database.EnsureCreatedAsync(); context.AddRange( new Blog { Name = "Blog1" }, new Blog { Name = "Blog2" }); await context.SaveChangesAsync(); } using (var context = new BlogsContext()) { var blogs = await context.Blogs.ToListAsync(); } }
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 }
protected BaseManager(BlogsContext context) { _context = context; }
public UserHelper(BlogsContext blogsContext) { _blogsContext = blogsContext; }
public BlogHelper(BlogsContext blogsContext) { _blogsContext = blogsContext; }
public void TimeToFirstQuery() { using var context = new BlogsContext(); var results = context.Set <Blog0000>().ToList(); }
public BlogsController(BlogsContext context) { _context = context; }
private void SeedData() { // Insert seed data into the database using one instance of the context using (var context = new BlogsContext(_dbContextOptions)) { var authorId = new Guid(); var userId = new Guid(); var blogId = new Guid(); var postId = new Guid(); var commentId = new Guid(); var blogs = new List <Blog> { new Blog() { Name = "Dummy blog", Posts = new List <Post>() { new Post() { PostId = new Guid(), BlogId = blogId, Comments = null, Content = "blog post", Title = "blog post title" } }, AuthorId = authorId, BlogId = blogId } }; foreach (var b in blogs) { context.Blogs.Add(b); } var authors = new List <Author> { new Author() { AuthorId = authorId, UserId = userId, Name = "AuthorFoo" } }; foreach (var a in authors) { context.Authors.Add(a); } var users = new List <User> { new User() { Password = "******", UserId = userId, Username = "******" } }; foreach (var u in users) { context.Users.Add(u); } var comments = new List <Comment>() { new Comment() { AuthorName = "fooCommentator", CommentId = commentId, Content = "first comment", CreatedOn = DateTime.Now, Email = "*****@*****.**", PostId = postId } }; foreach (var c in comments) { context.Comments.Add(c); } var posts = new List <Post> { new Post { PostId = postId, BlogId = blogId, Comments = comments, Createdon = DateTime.Now, Content = "Dummy post content", Title = "Dummy post title" }, new Post { PostId = new Guid(), BlogId = blogId, Comments = null, Createdon = DateTime.Now, Content = "Dummy post content", Title = "Dummy post title" } }; foreach (var p in posts) { context.Posts.Add(p); } context.SaveChanges(); blogsContext = context; } }
public Reposirory(BlogsContext blogContext) { this.db = blogContext; this.entities = blogContext.Set <T>(); }
public BlogController(BlogsContext db, IHostingEnvironment hostingEnvironment, UserManager <IdentityUser> userManager) { this.db = db; this.userManager = userManager; _hostingEnvironment = hostingEnvironment; }
public static void PopulateDatabase() { using var context = new BlogsContext(LogLevel.Error); var blogs = new[] { 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..." }, } } }; var tags = new[] { new Tag { Text = ".NET", Posts = { blogs[0].Posts[0], blogs[0].Posts[1] } }, new Tag { Text = "Visual Studio", Posts = { blogs[1].Posts[0], blogs[1].Posts[1] } }, new Tag { Text = "EF Core", Posts = { blogs[0].Posts[0] } } }; context.AddRange(blogs); context.AddRange(tags); context.SaveChanges(); }
public HomeController(BlogsContext db, UserManager <IdentityUser> userManager, SignInManager <IdentityUser> signInManager) { this.db = db; this.userManager = userManager; this.signInManager = signInManager; }
public BlogsController(BlogsContext blogsContext) { _blogsContext = blogsContext; _htmlSanitizer = new HtmlSanitizer(); }
public AuthorsEntityFrameworkRepository(BlogsContext context) : base(context) { }
public PostHelper(BlogsContext blogsContext) { _blogsContext = blogsContext; }
public PostManager(BlogsContext context) : base(context) { }
public PostsEntityFrameworkRepository(BlogsContext context) : base(context) { }