public static async Task Main(string[] args) { using (var db = new BloggingContext()) { // Create Console.WriteLine("Inserting a new blog"); await db.AddAsync(new Blog { Url = "http://blogs.msdn.com/adonet" }); await db.SaveChangesAsync(); // Read Console.WriteLine("Querying for a blog"); var blog = db.Blogs .OrderBy(b => b.BlogId) .First(); // Update Console.WriteLine("Updating the blog and adding a post"); blog.Url = "https://devblogs.microsoft.com/dotnet"; blog.Posts.Add( new Post { Title = "Hello World", Content = "I wrote an app using EF Core!" }); await db.SaveChangesAsync(); // Delete Console.WriteLine("Delete the blog"); db.Remove(blog); await db.SaveChangesAsync(); } }
public async Task <Blog> CreateBlogAsync(Blog blog) { using (var db = new BloggingContext()) { await db.AddAsync(blog); var result = await db.SaveChangesAsync(); var newBlog = await db.Blogs.FirstAsync(b => b.BlogId == blog.BlogId); var newPosts = await db.Posts.Where(p => p.BlogId == newBlog.BlogId).ToListAsync(); newBlog.Posts = newPosts; return(newBlog); } }