Compare commits
6 Commits
dd195dfddf
...
56fd0e44e7
| Author | SHA1 | Date | |
|---|---|---|---|
| 56fd0e44e7 | |||
| 73187855ba | |||
| 1c48a8c9c9 | |||
| 001e78736c | |||
| 093c663b31 | |||
| 99edd6c536 |
13
EntityFrameworkCore.API/EntityFrameworkCore.API.csproj
Normal file
13
EntityFrameworkCore.API/EntityFrameworkCore.API.csproj
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<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>
|
||||||
6
EntityFrameworkCore.API/EntityFrameworkCore.API.http
Normal file
6
EntityFrameworkCore.API/EntityFrameworkCore.API.http
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
@EntityFrameworkCore.API_HostAddress = http://localhost:5072
|
||||||
|
|
||||||
|
GET {{EntityFrameworkCore.API_HostAddress}}/weatherforecast/
|
||||||
|
Accept: application/json
|
||||||
|
|
||||||
|
###
|
||||||
41
EntityFrameworkCore.API/Program.cs
Normal file
41
EntityFrameworkCore.API/Program.cs
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
23
EntityFrameworkCore.API/Properties/launchSettings.json
Normal file
23
EntityFrameworkCore.API/Properties/launchSettings.json
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
{
|
||||||
|
"$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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
8
EntityFrameworkCore.API/appsettings.Development.json
Normal file
8
EntityFrameworkCore.API/appsettings.Development.json
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
9
EntityFrameworkCore.API/appsettings.json
Normal file
9
EntityFrameworkCore.API/appsettings.json
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"AllowedHosts": "*"
|
||||||
|
}
|
||||||
@ -1,5 +1,6 @@
|
|||||||
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();
|
||||||
@ -15,7 +16,93 @@ 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,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();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,6 +9,8 @@ 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
|
||||||
@ -30,5 +32,9 @@ 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