Add example user-defined function.

This commit is contained in:
Kevin Matsubara 2025-04-08 22:35:20 +02:00
parent 093c663b31
commit 001e78736c
2 changed files with 26 additions and 0 deletions

View File

@ -17,6 +17,21 @@ context.Database.EnsureCreated();
// var firstCoach = await context.Coaches.FirstOrDefaultAsync(); // var firstCoach = await context.Coaches.FirstOrDefaultAsync();
async Task UserDefinedQuery()
{
var earliestMatch = context.GetEarliestTeamMatch(1);
// This is done, if you have a user-defined function in the database.
// for example:
// CREATE FUNCTION [dbo].[GetEarliestMatch] (@teamId int)
// RETURNS datetime
// BEGIN
// DECLARE @result datetime
// SELECT TOP 1 @result = date
// FROM MATCHES [dbo].[Matches]
// ORDER BY Date
// return @result
// END
}
async Task QueryScalarType() async Task QueryScalarType()
{ {

View File

@ -41,5 +41,16 @@ public class DeadBallZoneLeagueDbContext : DbContext
// This database object does not have a Primary Key, so we state that here, otherwise an exception is thrown. // This database object does not have a Primary Key, so we state that here, otherwise an exception is thrown.
modelBuilder.Entity<TeamsAndLeaguesView>().HasNoKey().ToView("vw_TeamsAndLeagues"); modelBuilder.Entity<TeamsAndLeaguesView>().HasNoKey().ToView("vw_TeamsAndLeagues");
// This is for user-defined functions.
modelBuilder.HasDbFunction(
typeof(DeadBallZoneLeagueDbContext)
.GetMethod(
nameof(GetEarliestTeamMatch),
new[] {typeof(int)}
))
.HasName("GetEarliestMatch");
} }
public DateTime GetEarliestTeamMatch(int teamId) => throw new NotImplementedException();
} }