Add example function inserting relational data objects.
This commit is contained in:
parent
b56a19d8b8
commit
6dcf07e90e
@ -15,6 +15,64 @@ 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();
|
||||||
|
|
||||||
|
async Task InsertRelatedData()
|
||||||
|
{
|
||||||
|
// Insert record with a Foreign Key.
|
||||||
|
var match = new Match
|
||||||
|
{
|
||||||
|
AwayTeamId = 1,
|
||||||
|
HomeTeamId = 2,
|
||||||
|
AwayTeamScore = 0,
|
||||||
|
HomeTeamScore = 0,
|
||||||
|
Date = new DateTime(2025, 6, 1),
|
||||||
|
TicketPrice = 20
|
||||||
|
};
|
||||||
|
|
||||||
|
await context.AddAsync(match);
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
|
||||||
|
// Insert Parent/Child
|
||||||
|
var team = new Team
|
||||||
|
{
|
||||||
|
Name = "Neo Istanbul",
|
||||||
|
Coach = new Coach
|
||||||
|
{
|
||||||
|
Name = "Gimbal Wizbouski"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
await context.AddAsync(team);
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
|
||||||
|
// Insert Parent with Children.
|
||||||
|
var league = new League
|
||||||
|
{
|
||||||
|
Name = "Star League",
|
||||||
|
Teams = new List<Team>
|
||||||
|
{
|
||||||
|
new Team
|
||||||
|
{
|
||||||
|
Name = "Neo Cairo",
|
||||||
|
Coach = new Coach
|
||||||
|
{
|
||||||
|
Name = "Damon Pulsar"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
new Team
|
||||||
|
{
|
||||||
|
Name = "Cyborgs",
|
||||||
|
Coach = new Coach
|
||||||
|
{
|
||||||
|
Name = "C-1"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
await context.AddAsync(league);
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
}
|
||||||
|
|
||||||
async Task ExecuteDelete(string name)
|
async Task ExecuteDelete(string name)
|
||||||
{
|
{
|
||||||
// var coaches = await context.Coaches.Where(c => c.Name == name).ToListAsync();
|
// var coaches = await context.Coaches.Where(c => c.Name == name).ToListAsync();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user