Skip to content

Instantly share code, notes, and snippets.

@matisiekpl
Created June 14, 2023 19:21
Show Gist options
  • Save matisiekpl/a240a28731f83ad59d9f54b3c2c3f1bb to your computer and use it in GitHub Desktop.
Save matisiekpl/a240a28731f83ad59d9f54b3c2c3f1bb to your computer and use it in GitHub Desktop.
using Microsoft.EntityFrameworkCore;
namespace WebApplication6;
public class DatabaseContext: DbContext
{
public DbSet<Match> Matches { get; set; }
public DbSet<Team> Teams { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) =>
optionsBuilder
.UseMySQL(Environment.GetEnvironmentVariable("MYSQL_CONNECTION") ??
"server=localhost;database=efcoretest;user=root;password=12345678")
.UseLoggerFactory(LoggerFactory.Create(builder => builder.AddConsole()))
.EnableSensitiveDataLogging();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Match>().Property(x => x.GuestTeamId).IsRequired(false);
modelBuilder.Entity<Match>().Property(x => x.HomeTeamId).IsRequired(false);
modelBuilder.Entity<Match>()
.HasOne(x => x.HomeTeam)
.WithMany(e => e.HomeMatches)
.HasForeignKey(m => m.HomeTeamId)
.OnDelete(DeleteBehavior.ClientSetNull);
modelBuilder.Entity<Match>()
.HasOne(x => x.GuestTeam)
.WithMany(e => e.AwayMatches)
.HasForeignKey(m => m.GuestTeamId)
.OnDelete(DeleteBehavior.ClientSetNull);
}
}
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace WebApplication6;
public class Match
{
[Key]
public int MatchId { get; set; }
[ForeignKey("HomeTeam"), Column(Order = 0)]
public int? HomeTeamId { get; set; }
[ForeignKey("GuestTeam"), Column(Order = 1)]
public int? GuestTeamId { get; set; }
public float HomePoints { get; set; }
public float GuestPoints { get; set; }
public DateTime Date { get; set; }
public virtual Team HomeTeam { get; set; }
public virtual Team GuestTeam { get; set; }
}
using Microsoft.EntityFrameworkCore;
using WebApplication6;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddDbContext<DatabaseContext>(ServiceLifetime.Transient);
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
using var scope = app.Services.CreateScope();
var services = scope.ServiceProvider;
var context = services.GetRequiredService<DatabaseContext>();
if (context.Database.GetPendingMigrations().Any())
context.Database.Migrate();
var team1 = new Team
{
Name = "Team 1",
};
var team2 = new Team
{
Name = "Team 2",
};
var match = new Match
{
HomeTeam = team1,
GuestTeam = team2,
HomePoints = 1,
GuestPoints = 2,
Date = DateTime.Now,
};
context.Teams.Add(team1);
context.Teams.Add(team2);
context.Matches.Add(match);
context.SaveChanges();
context.Teams.Remove(team1);
context.SaveChanges();
foreach (var team in context.Teams.ToList())
Console.WriteLine(team.Name);
foreach (var m in context.Matches.ToList())
Console.WriteLine(m.MatchId);
using System.ComponentModel.DataAnnotations;
namespace WebApplication6;
public class Team
{
[Key]
public int TeamId { get; set;}
public string Name { get; set; }
public virtual ICollection<Match> HomeMatches { get; set; }
public virtual ICollection<Match> AwayMatches { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment