From e83f4acd6d9e7e9711430f640c9c10a6d3f46dbe Mon Sep 17 00:00:00 2001 From: Jiri Cincura Date: Fri, 22 Nov 2024 10:20:54 +0100 Subject: [PATCH 1/2] Update code to net9.0. --- .../samples/cu90/ContosoUniversity.csproj | 12 ++-- .../intro/samples/cu90/Data/DbInitializer.cs | 63 +++++++++++++++++ .../intro/samples/cu90/Data/DbInitializer1.cs | 69 ------------------- .../intro/samples/cu90/Data/SchoolContext.cs | 2 +- .../intro/samples/cu90/Models/Department.cs | 9 +-- 5 files changed, 73 insertions(+), 82 deletions(-) create mode 100644 aspnetcore/data/ef-rp/intro/samples/cu90/Data/DbInitializer.cs delete mode 100644 aspnetcore/data/ef-rp/intro/samples/cu90/Data/DbInitializer1.cs diff --git a/aspnetcore/data/ef-rp/intro/samples/cu90/ContosoUniversity.csproj b/aspnetcore/data/ef-rp/intro/samples/cu90/ContosoUniversity.csproj index 349140201d60..e17d8c07037b 100644 --- a/aspnetcore/data/ef-rp/intro/samples/cu90/ContosoUniversity.csproj +++ b/aspnetcore/data/ef-rp/intro/samples/cu90/ContosoUniversity.csproj @@ -1,7 +1,7 @@  - net6.0 + net9.0 enable @@ -11,14 +11,14 @@ - - - - + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/aspnetcore/data/ef-rp/intro/samples/cu90/Data/DbInitializer.cs b/aspnetcore/data/ef-rp/intro/samples/cu90/Data/DbInitializer.cs new file mode 100644 index 000000000000..0fd09e8956af --- /dev/null +++ b/aspnetcore/data/ef-rp/intro/samples/cu90/Data/DbInitializer.cs @@ -0,0 +1,63 @@ +#define FIRST +#if FIRST // First DbInitializer used +// +using ContosoUniversity.Models; + +namespace ContosoUniversity.Data +{ + public static class DbInitializer + { + public static void Initialize(SchoolContext context) + { + // Look for any students. + if (context.Students.Any()) + { + return; // DB has been seeded + } + + context.Students.AddRange( + [ + new() { FirstMidName = "Carson", LastName = "Alexander", EnrollmentDate = DateTime.Parse("2019-09-01") }, + new() { FirstMidName = "Meredith", LastName = "Alonso", EnrollmentDate = DateTime.Parse("2017-09-01") }, + new() { FirstMidName = "Arturo", LastName = "Anand", EnrollmentDate = DateTime.Parse("2018-09-01") }, + new() { FirstMidName = "Gytis", LastName = "Barzdukas", EnrollmentDate = DateTime.Parse("2017-09-01") }, + new() { FirstMidName = "Yan", LastName = "Li", EnrollmentDate = DateTime.Parse("2017-09-01") }, + new() { FirstMidName = "Peggy", LastName = "Justice", EnrollmentDate = DateTime.Parse("2016-09-01") }, + new() { FirstMidName = "Laura", LastName = "Norman", EnrollmentDate = DateTime.Parse("2018-09-01") }, + new() { FirstMidName = "Nino", LastName = "Olivetto", EnrollmentDate = DateTime.Parse("2019-09-01") }, + ]); + context.SaveChanges(); + + context.Courses.AddRange( + [ + new() { CourseID = 1050, Title = "Chemistry", Credits = 3 }, + new() { CourseID = 4022, Title = "Microeconomics", Credits = 3 }, + new() { CourseID = 4041, Title = "Macroeconomics", Credits = 3 }, + new() { CourseID = 1045, Title = "Calculus", Credits = 4 }, + new() { CourseID = 3141, Title = "Trigonometry", Credits = 4 }, + new() { CourseID = 2021, Title = "Composition", Credits = 3 }, + new() { CourseID = 2042, Title = "Literature", Credits = 4 }, + ]); + context.SaveChanges(); + + context.Enrollments.AddRange( + [ + new() { StudentID = 1, CourseID = 1050, Grade = Grade.A }, + new() { StudentID = 1, CourseID = 4022, Grade = Grade.C }, + new() { StudentID = 1, CourseID = 4041, Grade = Grade.B }, + new() { StudentID = 2, CourseID = 1045, Grade = Grade.B }, + new() { StudentID = 2, CourseID = 3141, Grade = Grade.F }, + new() { StudentID = 2, CourseID = 2021, Grade = Grade.F }, + new() { StudentID = 3, CourseID = 1050 }, + new() { StudentID = 4, CourseID = 1050 }, + new() { StudentID = 4, CourseID = 4022, Grade = Grade.F}, + new() { StudentID = 5, CourseID = 4041, Grade = Grade.C}, + new() { StudentID = 6, CourseID = 1045 }, + new() { StudentID = 7, CourseID = 3141, Grade = Grade.A}, + ]); + context.SaveChanges(); + } + } +} +// +#endif diff --git a/aspnetcore/data/ef-rp/intro/samples/cu90/Data/DbInitializer1.cs b/aspnetcore/data/ef-rp/intro/samples/cu90/Data/DbInitializer1.cs deleted file mode 100644 index 2d65c61fa7c4..000000000000 --- a/aspnetcore/data/ef-rp/intro/samples/cu90/Data/DbInitializer1.cs +++ /dev/null @@ -1,69 +0,0 @@ -#define FIRST -#if FIRST // First DbInitializer used -// -using ContosoUniversity.Models; - -namespace ContosoUniversity.Data -{ - public static class DbInitializer - { - public static void Initialize(SchoolContext context) - { - // Look for any students. - if (context.Students.Any()) - { - return; // DB has been seeded - } - - var students = new Student[] - { - new Student{FirstMidName="Carson",LastName="Alexander",EnrollmentDate=DateTime.Parse("2019-09-01")}, - new Student{FirstMidName="Meredith",LastName="Alonso",EnrollmentDate=DateTime.Parse("2017-09-01")}, - new Student{FirstMidName="Arturo",LastName="Anand",EnrollmentDate=DateTime.Parse("2018-09-01")}, - new Student{FirstMidName="Gytis",LastName="Barzdukas",EnrollmentDate=DateTime.Parse("2017-09-01")}, - new Student{FirstMidName="Yan",LastName="Li",EnrollmentDate=DateTime.Parse("2017-09-01")}, - new Student{FirstMidName="Peggy",LastName="Justice",EnrollmentDate=DateTime.Parse("2016-09-01")}, - new Student{FirstMidName="Laura",LastName="Norman",EnrollmentDate=DateTime.Parse("2018-09-01")}, - new Student{FirstMidName="Nino",LastName="Olivetto",EnrollmentDate=DateTime.Parse("2019-09-01")} - }; - - context.Students.AddRange(students); - context.SaveChanges(); - - var courses = new Course[] - { - new Course{CourseID=1050,Title="Chemistry",Credits=3}, - new Course{CourseID=4022,Title="Microeconomics",Credits=3}, - new Course{CourseID=4041,Title="Macroeconomics",Credits=3}, - new Course{CourseID=1045,Title="Calculus",Credits=4}, - new Course{CourseID=3141,Title="Trigonometry",Credits=4}, - new Course{CourseID=2021,Title="Composition",Credits=3}, - new Course{CourseID=2042,Title="Literature",Credits=4} - }; - - context.Courses.AddRange(courses); - context.SaveChanges(); - - var enrollments = new Enrollment[] - { - new Enrollment{StudentID=1,CourseID=1050,Grade=Grade.A}, - new Enrollment{StudentID=1,CourseID=4022,Grade=Grade.C}, - new Enrollment{StudentID=1,CourseID=4041,Grade=Grade.B}, - new Enrollment{StudentID=2,CourseID=1045,Grade=Grade.B}, - new Enrollment{StudentID=2,CourseID=3141,Grade=Grade.F}, - new Enrollment{StudentID=2,CourseID=2021,Grade=Grade.F}, - new Enrollment{StudentID=3,CourseID=1050}, - new Enrollment{StudentID=4,CourseID=1050}, - new Enrollment{StudentID=4,CourseID=4022,Grade=Grade.F}, - new Enrollment{StudentID=5,CourseID=4041,Grade=Grade.C}, - new Enrollment{StudentID=6,CourseID=1045}, - new Enrollment{StudentID=7,CourseID=3141,Grade=Grade.A}, - }; - - context.Enrollments.AddRange(enrollments); - context.SaveChanges(); - } - } -} -// -#endif diff --git a/aspnetcore/data/ef-rp/intro/samples/cu90/Data/SchoolContext.cs b/aspnetcore/data/ef-rp/intro/samples/cu90/Data/SchoolContext.cs index 8c6350087ee4..d463b1efef02 100644 --- a/aspnetcore/data/ef-rp/intro/samples/cu90/Data/SchoolContext.cs +++ b/aspnetcore/data/ef-rp/intro/samples/cu90/Data/SchoolContext.cs @@ -8,7 +8,7 @@ namespace ContosoUniversity.Data { public class SchoolContext : DbContext { - public SchoolContext (DbContextOptions options) + public SchoolContext(DbContextOptions options) : base(options) { } diff --git a/aspnetcore/data/ef-rp/intro/samples/cu90/Models/Department.cs b/aspnetcore/data/ef-rp/intro/samples/cu90/Models/Department.cs index 7d3834ed5d98..2e77936ca685 100644 --- a/aspnetcore/data/ef-rp/intro/samples/cu90/Models/Department.cs +++ b/aspnetcore/data/ef-rp/intro/samples/cu90/Models/Department.cs @@ -18,8 +18,7 @@ public class Department public decimal Budget { get; set; } [DataType(DataType.Date)] - [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", - ApplyFormatInEditMode = true)] + [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] [Display(Name = "Start Date")] public DateTime StartDate { get; set; } @@ -52,8 +51,7 @@ public class Department public decimal Budget { get; set; } [DataType(DataType.Date)] - [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", - ApplyFormatInEditMode = true)] + [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] [Display(Name = "Start Date")] public DateTime StartDate { get; set; } @@ -87,8 +85,7 @@ public class Department public decimal Budget { get; set; } [DataType(DataType.Date)] - [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", - ApplyFormatInEditMode = true)] + [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] [Display(Name = "Start Date")] public DateTime StartDate { get; set; } From d19bb48184da00e680e3c4b277478eafe0d69906 Mon Sep 17 00:00:00 2001 From: Jiri Cincura Date: Fri, 22 Nov 2024 11:08:20 +0100 Subject: [PATCH 2/2] Use async for StudentExists. --- .../intro/samples/cu90/Pages/Students/Edit.cshtml.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/aspnetcore/data/ef-rp/intro/samples/cu90/Pages/Students/Edit.cshtml.cs b/aspnetcore/data/ef-rp/intro/samples/cu90/Pages/Students/Edit.cshtml.cs index 2a9d0b299e2e..b436a2cb17db 100644 --- a/aspnetcore/data/ef-rp/intro/samples/cu90/Pages/Students/Edit.cshtml.cs +++ b/aspnetcore/data/ef-rp/intro/samples/cu90/Pages/Students/Edit.cshtml.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -56,7 +56,7 @@ public async Task OnPostAsync() } catch (DbUpdateConcurrencyException) { - if (!StudentExists(Student.ID)) + if (!await StudentExistsAsync(Student.ID)) { return NotFound(); } @@ -69,9 +69,9 @@ public async Task OnPostAsync() return RedirectToPage("./Index"); } - private bool StudentExists(int id) + private Task StudentExistsAsync(int id) { - return _context.Students.Any(e => e.ID == id); + return _context.Students.AnyAsync(e => e.ID == id); } } }