Skip to content

Commit 75cf531

Browse files
Merge pull request #34203 from cincuranet/issue-4633
Update EF code to .NET 9
2 parents da2307f + d19bb48 commit 75cf531

File tree

6 files changed

+77
-86
lines changed

6 files changed

+77
-86
lines changed
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFramework>net9.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
</PropertyGroup>
77

@@ -11,14 +11,14 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="6.0.0" />
15-
<PackageReference Include="Microsoft.EntityFrameworkCore.SQLite" Version="6.0.0" />
16-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
17-
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0">
14+
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="9.0.0" />
15+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.0" />
16+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0" />
17+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.0">
1818
<PrivateAssets>all</PrivateAssets>
1919
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2020
</PackageReference>
21-
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.0" />
21+
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="9.0.0" />
2222
</ItemGroup>
2323

2424
</Project>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#define FIRST
2+
#if FIRST // First DbInitializer used
3+
// <snippet>
4+
using ContosoUniversity.Models;
5+
6+
namespace ContosoUniversity.Data
7+
{
8+
public static class DbInitializer
9+
{
10+
public static void Initialize(SchoolContext context)
11+
{
12+
// Look for any students.
13+
if (context.Students.Any())
14+
{
15+
return; // DB has been seeded
16+
}
17+
18+
context.Students.AddRange(
19+
[
20+
new() { FirstMidName = "Carson", LastName = "Alexander", EnrollmentDate = DateTime.Parse("2019-09-01") },
21+
new() { FirstMidName = "Meredith", LastName = "Alonso", EnrollmentDate = DateTime.Parse("2017-09-01") },
22+
new() { FirstMidName = "Arturo", LastName = "Anand", EnrollmentDate = DateTime.Parse("2018-09-01") },
23+
new() { FirstMidName = "Gytis", LastName = "Barzdukas", EnrollmentDate = DateTime.Parse("2017-09-01") },
24+
new() { FirstMidName = "Yan", LastName = "Li", EnrollmentDate = DateTime.Parse("2017-09-01") },
25+
new() { FirstMidName = "Peggy", LastName = "Justice", EnrollmentDate = DateTime.Parse("2016-09-01") },
26+
new() { FirstMidName = "Laura", LastName = "Norman", EnrollmentDate = DateTime.Parse("2018-09-01") },
27+
new() { FirstMidName = "Nino", LastName = "Olivetto", EnrollmentDate = DateTime.Parse("2019-09-01") },
28+
]);
29+
context.SaveChanges();
30+
31+
context.Courses.AddRange(
32+
[
33+
new() { CourseID = 1050, Title = "Chemistry", Credits = 3 },
34+
new() { CourseID = 4022, Title = "Microeconomics", Credits = 3 },
35+
new() { CourseID = 4041, Title = "Macroeconomics", Credits = 3 },
36+
new() { CourseID = 1045, Title = "Calculus", Credits = 4 },
37+
new() { CourseID = 3141, Title = "Trigonometry", Credits = 4 },
38+
new() { CourseID = 2021, Title = "Composition", Credits = 3 },
39+
new() { CourseID = 2042, Title = "Literature", Credits = 4 },
40+
]);
41+
context.SaveChanges();
42+
43+
context.Enrollments.AddRange(
44+
[
45+
new() { StudentID = 1, CourseID = 1050, Grade = Grade.A },
46+
new() { StudentID = 1, CourseID = 4022, Grade = Grade.C },
47+
new() { StudentID = 1, CourseID = 4041, Grade = Grade.B },
48+
new() { StudentID = 2, CourseID = 1045, Grade = Grade.B },
49+
new() { StudentID = 2, CourseID = 3141, Grade = Grade.F },
50+
new() { StudentID = 2, CourseID = 2021, Grade = Grade.F },
51+
new() { StudentID = 3, CourseID = 1050 },
52+
new() { StudentID = 4, CourseID = 1050 },
53+
new() { StudentID = 4, CourseID = 4022, Grade = Grade.F},
54+
new() { StudentID = 5, CourseID = 4041, Grade = Grade.C},
55+
new() { StudentID = 6, CourseID = 1045 },
56+
new() { StudentID = 7, CourseID = 3141, Grade = Grade.A},
57+
]);
58+
context.SaveChanges();
59+
}
60+
}
61+
}
62+
// </snippet>
63+
#endif

aspnetcore/data/ef-rp/intro/samples/cu90/Data/DbInitializer1.cs

Lines changed: 0 additions & 69 deletions
This file was deleted.

aspnetcore/data/ef-rp/intro/samples/cu90/Data/SchoolContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace ContosoUniversity.Data
88
{
99
public class SchoolContext : DbContext
1010
{
11-
public SchoolContext (DbContextOptions<SchoolContext> options)
11+
public SchoolContext(DbContextOptions<SchoolContext> options)
1212
: base(options)
1313
{
1414
}

aspnetcore/data/ef-rp/intro/samples/cu90/Models/Department.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ public class Department
1818
public decimal Budget { get; set; }
1919

2020
[DataType(DataType.Date)]
21-
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}",
22-
ApplyFormatInEditMode = true)]
21+
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
2322
[Display(Name = "Start Date")]
2423
public DateTime StartDate { get; set; }
2524

@@ -52,8 +51,7 @@ public class Department
5251
public decimal Budget { get; set; }
5352

5453
[DataType(DataType.Date)]
55-
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}",
56-
ApplyFormatInEditMode = true)]
54+
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
5755
[Display(Name = "Start Date")]
5856
public DateTime StartDate { get; set; }
5957

@@ -87,8 +85,7 @@ public class Department
8785
public decimal Budget { get; set; }
8886

8987
[DataType(DataType.Date)]
90-
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}",
91-
ApplyFormatInEditMode = true)]
88+
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
9289
[Display(Name = "Start Date")]
9390
public DateTime StartDate { get; set; }
9491

aspnetcore/data/ef-rp/intro/samples/cu90/Pages/Students/Edit.cshtml.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Threading.Tasks;
@@ -56,7 +56,7 @@ public async Task<IActionResult> OnPostAsync()
5656
}
5757
catch (DbUpdateConcurrencyException)
5858
{
59-
if (!StudentExists(Student.ID))
59+
if (!await StudentExistsAsync(Student.ID))
6060
{
6161
return NotFound();
6262
}
@@ -69,9 +69,9 @@ public async Task<IActionResult> OnPostAsync()
6969
return RedirectToPage("./Index");
7070
}
7171

72-
private bool StudentExists(int id)
72+
private Task<bool> StudentExistsAsync(int id)
7373
{
74-
return _context.Students.Any(e => e.ID == id);
74+
return _context.Students.AnyAsync(e => e.ID == id);
7575
}
7676
}
7777
}

0 commit comments

Comments
 (0)