From 61b0e99f947a9dad152ac8ddf30661c60177ad74 Mon Sep 17 00:00:00 2001 From: Kevin Matsubara Date: Sat, 5 Apr 2025 00:19:28 +0200 Subject: [PATCH] Create example queries in Console project. --- EntityFrameworkCore.Console/Program.cs | 34 ++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/EntityFrameworkCore.Console/Program.cs b/EntityFrameworkCore.Console/Program.cs index 3751555..fa20316 100644 --- a/EntityFrameworkCore.Console/Program.cs +++ b/EntityFrameworkCore.Console/Program.cs @@ -1,2 +1,32 @@ -// See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); +using EntityFrameworkCore.Data; +using Microsoft.EntityFrameworkCore; + +using var context = new DeadBallZoneLeagueDbContext(); + +//PrintTeams(); + +var teamOne = await context.Teams.FirstAsync(t => t.TeamId == 1); +var teamTwo = await context.Teams.FirstAsync(t => t.TeamId == 2); +var teamThree = await context.Teams.SingleAsync(t => t.TeamId == 3); +var teamFour = await context.Teams.SingleOrDefaultAsync(t => t.TeamId == 3); + +// There is no entry for coaches yet, this function will provide a null to coach. +// Rather than raise: "System.InvalidOperationException: Sequence contains no elements." +var coach = await context.Coaches.FirstOrDefaultAsync(); + +// Selection based on finding the Primary Key. +var teamBasedOnId = await context.Teams.FindAsync(5); +if (teamBasedOnId != null) +{ + Console.WriteLine(teamBasedOnId.Name); +} + +void PrintTeams() +{ + var teams = context.Teams.ToList(); + + foreach (var team in teams) + { + Console.WriteLine(team.Name); + } +}