Compare commits
No commits in common. "56fd0e44e7485d25ef82265f641c4540965fcd02" and "dd195dfddf972add0c49805b65c0a5c3940ce534" have entirely different histories.
56fd0e44e7
...
dd195dfddf
@ -1,13 +0,0 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
|
||||||
|
|
||||||
<PropertyGroup>
|
|
||||||
<TargetFramework>net9.0</TargetFramework>
|
|
||||||
<Nullable>enable</Nullable>
|
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.3" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
|
||||||
@ -1,6 +0,0 @@
|
|||||||
@EntityFrameworkCore.API_HostAddress = http://localhost:5072
|
|
||||||
|
|
||||||
GET {{EntityFrameworkCore.API_HostAddress}}/weatherforecast/
|
|
||||||
Accept: application/json
|
|
||||||
|
|
||||||
###
|
|
||||||
@ -1,41 +0,0 @@
|
|||||||
var builder = WebApplication.CreateBuilder(args);
|
|
||||||
|
|
||||||
// Add services to the container.
|
|
||||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
|
||||||
builder.Services.AddOpenApi();
|
|
||||||
|
|
||||||
var app = builder.Build();
|
|
||||||
|
|
||||||
// Configure the HTTP request pipeline.
|
|
||||||
if (app.Environment.IsDevelopment())
|
|
||||||
{
|
|
||||||
app.MapOpenApi();
|
|
||||||
}
|
|
||||||
|
|
||||||
app.UseHttpsRedirection();
|
|
||||||
|
|
||||||
var summaries = new[]
|
|
||||||
{
|
|
||||||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
|
||||||
};
|
|
||||||
|
|
||||||
app.MapGet("/weatherforecast", () =>
|
|
||||||
{
|
|
||||||
var forecast = Enumerable.Range(1, 5).Select(index =>
|
|
||||||
new WeatherForecast
|
|
||||||
(
|
|
||||||
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
|
|
||||||
Random.Shared.Next(-20, 55),
|
|
||||||
summaries[Random.Shared.Next(summaries.Length)]
|
|
||||||
))
|
|
||||||
.ToArray();
|
|
||||||
return forecast;
|
|
||||||
})
|
|
||||||
.WithName("GetWeatherForecast");
|
|
||||||
|
|
||||||
app.Run();
|
|
||||||
|
|
||||||
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
|
|
||||||
{
|
|
||||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
|
||||||
}
|
|
||||||
@ -1,23 +0,0 @@
|
|||||||
{
|
|
||||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
|
||||||
"profiles": {
|
|
||||||
"http": {
|
|
||||||
"commandName": "Project",
|
|
||||||
"dotnetRunMessages": true,
|
|
||||||
"launchBrowser": false,
|
|
||||||
"applicationUrl": "http://localhost:5072",
|
|
||||||
"environmentVariables": {
|
|
||||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"https": {
|
|
||||||
"commandName": "Project",
|
|
||||||
"dotnetRunMessages": true,
|
|
||||||
"launchBrowser": false,
|
|
||||||
"applicationUrl": "https://localhost:7223;http://localhost:5072",
|
|
||||||
"environmentVariables": {
|
|
||||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,8 +0,0 @@
|
|||||||
{
|
|
||||||
"Logging": {
|
|
||||||
"LogLevel": {
|
|
||||||
"Default": "Information",
|
|
||||||
"Microsoft.AspNetCore": "Warning"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,9 +0,0 @@
|
|||||||
{
|
|
||||||
"Logging": {
|
|
||||||
"LogLevel": {
|
|
||||||
"Default": "Information",
|
|
||||||
"Microsoft.AspNetCore": "Warning"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"AllowedHosts": "*"
|
|
||||||
}
|
|
||||||
@ -1,6 +1,5 @@
|
|||||||
using EntityFrameworkCore.Data;
|
using EntityFrameworkCore.Data;
|
||||||
using EntityFrameworkCore.Domain;
|
using EntityFrameworkCore.Domain;
|
||||||
using Microsoft.Data.Sqlite;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
using var context = new DeadBallZoneLeagueDbContext();
|
using var context = new DeadBallZoneLeagueDbContext();
|
||||||
@ -16,93 +15,7 @@ context.Database.EnsureCreated();
|
|||||||
// Rather than raise: "System.InvalidOperationException: Sequence contains no elements."
|
// Rather than raise: "System.InvalidOperationException: Sequence contains no elements."
|
||||||
// var firstCoach = await context.Coaches.FirstOrDefaultAsync();
|
// var firstCoach = await context.Coaches.FirstOrDefaultAsync();
|
||||||
|
|
||||||
|
var details = await context.TeamsAndLeaguesView.ToListAsync();
|
||||||
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()
|
|
||||||
{
|
|
||||||
// Query a scalar or non-entity type.
|
|
||||||
// Scalar subqueries or scalar queries are queries that return exactly one column and one or zero records.
|
|
||||||
// Source: https://stackoverflow.com/questions/20424966/what-is-a-scalar-query
|
|
||||||
var leagueIds = context.Database.SqlQuery<int>(
|
|
||||||
$"SELECT Id FROM Leagues"
|
|
||||||
).ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
async Task RawSQLStatement()
|
|
||||||
{
|
|
||||||
// Beware that SQL Injection can be used on input here.
|
|
||||||
Console.WriteLine("Enter Team name: ");
|
|
||||||
var teamName = Console.ReadLine();
|
|
||||||
// If you omit this param creation, SQL injection is possible.
|
|
||||||
var teamNameParam = new SqliteParameter("teamName", teamName);
|
|
||||||
var teams = context.Teams.FromSqlRaw(
|
|
||||||
$"SELECT * FROM Teams WHERE name = @teamName", teamNameParam
|
|
||||||
);
|
|
||||||
foreach (var t in teams)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"SQL Raw Team: {t.Name}");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parameterization happens automatically for these two:
|
|
||||||
teams = context.Teams.FromSql($"SELECT * FROM Teams WHERE name = {teamName}");
|
|
||||||
foreach (var t in teams)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"SQL Raw Team: {t.Name}");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Note, that you cannot use JOINs in these SQL queries.
|
|
||||||
// Instead, the Include() function is used to add related data.
|
|
||||||
|
|
||||||
// FromSqlInterpolated is the revised form of FromSql, and a better choice.
|
|
||||||
teams = context.Teams.FromSqlInterpolated($"SELECT * FROM Teams WHERE name = {teamName}");
|
|
||||||
foreach (var t in teams)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"SQL Raw Team: {t.Name}");
|
|
||||||
}
|
|
||||||
|
|
||||||
// You can also mix with LINQ.
|
|
||||||
var teamsList = context.Teams.FromSql($"SELECT * FROM Teams")
|
|
||||||
.Where(t => t.Id == 1)
|
|
||||||
.OrderBy(t => t.Id)
|
|
||||||
.Include("League")
|
|
||||||
.ToList();
|
|
||||||
foreach (var t in teamsList)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"SQL Raw Team: {t.Name}");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stored procedures example. Note that SQLite does not support stored proceduces.
|
|
||||||
var leagueId = 1;
|
|
||||||
var league = context.Leagues.FromSqlInterpolated(
|
|
||||||
$"EXEC dbo.StoredProcedureToGetLeagueName {leagueId}"
|
|
||||||
);
|
|
||||||
|
|
||||||
// Non-querying statement examples.
|
|
||||||
var newTeamName = "Neo Oslo";
|
|
||||||
var success = context.Database.ExecuteSqlInterpolated(
|
|
||||||
$"UPDATE Teams SET Name = {newTeamName}"
|
|
||||||
);
|
|
||||||
|
|
||||||
var teamToDelete = 1;
|
|
||||||
var teamDeletedSuccess = context.Database.ExecuteSqlInterpolated(
|
|
||||||
$"EXEC dbo.DeleteTeam {teamToDelete}"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
async Task ProjectionAndAnonymousDataTypes()
|
async Task ProjectionAndAnonymousDataTypes()
|
||||||
{
|
{
|
||||||
|
|||||||
@ -41,16 +41,5 @@ 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();
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,8 +9,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityFrameworkCore.Domain"
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityFrameworkCore.Console", "EntityFrameworkCore.Console\EntityFrameworkCore.Console.csproj", "{58F305A5-1C40-445D-8B2D-2CA4A0224274}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityFrameworkCore.Console", "EntityFrameworkCore.Console\EntityFrameworkCore.Console.csproj", "{58F305A5-1C40-445D-8B2D-2CA4A0224274}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityFrameworkCore.API", "EntityFrameworkCore.API\EntityFrameworkCore.API.csproj", "{07BDB953-9BB3-431B-9F50-0AC270946B8D}"
|
|
||||||
EndProject
|
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@ -32,9 +30,5 @@ Global
|
|||||||
{58F305A5-1C40-445D-8B2D-2CA4A0224274}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{58F305A5-1C40-445D-8B2D-2CA4A0224274}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{58F305A5-1C40-445D-8B2D-2CA4A0224274}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{58F305A5-1C40-445D-8B2D-2CA4A0224274}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{58F305A5-1C40-445D-8B2D-2CA4A0224274}.Release|Any CPU.Build.0 = Release|Any CPU
|
{58F305A5-1C40-445D-8B2D-2CA4A0224274}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
{07BDB953-9BB3-431B-9F50-0AC270946B8D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
|
||||||
{07BDB953-9BB3-431B-9F50-0AC270946B8D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
|
||||||
{07BDB953-9BB3-431B-9F50-0AC270946B8D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
|
||||||
{07BDB953-9BB3-431B-9F50-0AC270946B8D}.Release|Any CPU.Build.0 = Release|Any CPU
|
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user