From c158680ce4a945b063c4cb8b328b459e3654c12f Mon Sep 17 00:00:00 2001 From: Daniel Niccoli <2971735+danielniccoli@users.noreply.github.com> Date: Fri, 13 May 2022 21:45:49 +0200 Subject: [PATCH] Top-level statements for EFGetStarted Migrate EFGetStarted sample to using top-level statements feature. (https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/program-structure/top-level-statements) --- samples/core/GetStarted/Model.cs | 6 ++-- samples/core/GetStarted/Program.cs | 52 +++++++++++++----------------- 2 files changed, 24 insertions(+), 34 deletions(-) diff --git a/samples/core/GetStarted/Model.cs b/samples/core/GetStarted/Model.cs index 06e097e76d..9865e3e735 100644 --- a/samples/core/GetStarted/Model.cs +++ b/samples/core/GetStarted/Model.cs @@ -1,8 +1,6 @@ -using System; +using Microsoft.EntityFrameworkCore; +using System; using System.Collections.Generic; -using Microsoft.EntityFrameworkCore; - -namespace EFGetStarted; public class BloggingContext : DbContext { diff --git a/samples/core/GetStarted/Program.cs b/samples/core/GetStarted/Program.cs index 597209a248..9b1e5538b6 100644 --- a/samples/core/GetStarted/Program.cs +++ b/samples/core/GetStarted/Program.cs @@ -1,38 +1,30 @@ using System; using System.Linq; -namespace EFGetStarted; +using var db = new BloggingContext(); -internal class Program -{ - private static void Main() - { - using var db = new BloggingContext(); +// Note: This sample requires the database to be created before running. +Console.WriteLine($"Database path: {db.DbPath}."); - // Note: This sample requires the database to be created before running. - Console.WriteLine($"Database path: {db.DbPath}."); +// Create +Console.WriteLine("Inserting a new blog"); +db.Add(new Blog { Url = "http://blogs.msdn.com/adonet" }); +db.SaveChanges(); - // Create - Console.WriteLine("Inserting a new blog"); - db.Add(new Blog { Url = "http://blogs.msdn.com/adonet" }); - db.SaveChanges(); +// Read +Console.WriteLine("Querying for a blog"); +var blog = db.Blogs + .OrderBy(b => b.BlogId) + .First(); - // 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!" }); +db.SaveChanges(); - // 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!" }); - db.SaveChanges(); - - // Delete - Console.WriteLine("Delete the blog"); - db.Remove(blog); - db.SaveChanges(); - } -} +// Delete +Console.WriteLine("Delete the blog"); +db.Remove(blog); +db.SaveChanges(); \ No newline at end of file