Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update EF code to .NET 9 #34203

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

Expand All @@ -11,14 +11,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SQLite" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0">
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="9.0.0" />
</ItemGroup>

</Project>
63 changes: 63 additions & 0 deletions aspnetcore/data/ef-rp/intro/samples/cu90/Data/DbInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#define FIRST
#if FIRST // First DbInitializer used
// <snippet>
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();
}
}
}
// </snippet>
#endif
69 changes: 0 additions & 69 deletions aspnetcore/data/ef-rp/intro/samples/cu90/Data/DbInitializer1.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace ContosoUniversity.Data
{
public class SchoolContext : DbContext
{
public SchoolContext (DbContextOptions<SchoolContext> options)
public SchoolContext(DbContextOptions<SchoolContext> options)
: base(options)
{
}
Expand Down
9 changes: 3 additions & 6 deletions aspnetcore/data/ef-rp/intro/samples/cu90/Models/Department.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down Expand Up @@ -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; }

Expand Down Expand Up @@ -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; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -56,7 +56,7 @@ public async Task<IActionResult> OnPostAsync()
}
catch (DbUpdateConcurrencyException)
{
if (!StudentExists(Student.ID))
if (!await StudentExistsAsync(Student.ID))
{
return NotFound();
}
Expand All @@ -69,9 +69,9 @@ public async Task<IActionResult> OnPostAsync()
return RedirectToPage("./Index");
}

private bool StudentExists(int id)
private Task<bool> StudentExistsAsync(int id)
{
return _context.Students.Any(e => e.ID == id);
return _context.Students.AnyAsync(e => e.ID == id);
}
}
}
Loading