PI06 i PI06-1. Docker definitions for MSSQL and Postgres. Data seeder/generator for countries and people. Entity Framework example with variants for Postgres and MSSQL
This commit is contained in:
143
DataAccess/EF_Demo/Data/MSSQL/EventsContext.cs
Normal file
143
DataAccess/EF_Demo/Data/MSSQL/EventsContext.cs
Normal file
@@ -0,0 +1,143 @@
|
||||
// <auto-generated> This file has been auto generated by EF Core Power Tools. </auto-generated>
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using EF_Demo.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace EF_Demo.Data.MSSQL;
|
||||
|
||||
public partial class EventsContext : DbContext
|
||||
{
|
||||
public EventsContext(DbContextOptions<EventsContext> options)
|
||||
: base(options)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual DbSet<Country> Countries { get; set; }
|
||||
|
||||
public virtual DbSet<Event> Events { get; set; }
|
||||
|
||||
public virtual DbSet<Person> People { get; set; }
|
||||
|
||||
public virtual DbSet<Registration> Registrations { get; set; }
|
||||
|
||||
public virtual DbSet<Sport> Sports { get; set; }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<Country>(entity =>
|
||||
{
|
||||
entity.HasKey(e => e.Code);
|
||||
|
||||
entity.ToTable("Country");
|
||||
|
||||
entity.HasIndex(e => e.Name, "UQ_Country_Name").IsUnique();
|
||||
|
||||
entity.Property(e => e.Code)
|
||||
.HasMaxLength(3)
|
||||
.IsUnicode(false);
|
||||
entity.Property(e => e.Alpha3)
|
||||
.HasMaxLength(3)
|
||||
.IsUnicode(false)
|
||||
.IsFixedLength();
|
||||
entity.Property(e => e.Name)
|
||||
.HasMaxLength(100)
|
||||
.IsUnicode(false);
|
||||
});
|
||||
|
||||
modelBuilder.Entity<Event>(entity =>
|
||||
{
|
||||
entity.ToTable("Event");
|
||||
|
||||
entity.Property(e => e.Name)
|
||||
.HasMaxLength(150)
|
||||
.IsUnicode(false);
|
||||
});
|
||||
|
||||
modelBuilder.Entity<Person>(entity =>
|
||||
{
|
||||
entity.ToTable("Person");
|
||||
|
||||
entity.HasIndex(e => new { e.DocumentNumber, e.CountryCode }, "UQ_Person_DocumentNumber_CountryCode").IsUnique();
|
||||
|
||||
entity.Property(e => e.AddressCountry)
|
||||
.HasMaxLength(100)
|
||||
.IsUnicode(false);
|
||||
entity.Property(e => e.AddressLine)
|
||||
.HasMaxLength(200)
|
||||
.IsUnicode(false);
|
||||
entity.Property(e => e.City)
|
||||
.HasMaxLength(100)
|
||||
.IsUnicode(false);
|
||||
entity.Property(e => e.ContactPhone)
|
||||
.HasMaxLength(50)
|
||||
.IsUnicode(false);
|
||||
entity.Property(e => e.CountryCode)
|
||||
.HasMaxLength(3)
|
||||
.IsUnicode(false);
|
||||
entity.Property(e => e.DocumentNumber)
|
||||
.HasMaxLength(50)
|
||||
.IsUnicode(false);
|
||||
entity.Property(e => e.Email)
|
||||
.HasMaxLength(255)
|
||||
.IsUnicode(false);
|
||||
entity.Property(e => e.FirstName)
|
||||
.HasMaxLength(100)
|
||||
.IsUnicode(false);
|
||||
entity.Property(e => e.FirstNameTranscription)
|
||||
.HasMaxLength(100)
|
||||
.IsUnicode(false);
|
||||
entity.Property(e => e.LastName)
|
||||
.HasMaxLength(100)
|
||||
.IsUnicode(false);
|
||||
entity.Property(e => e.LastNameTranscription)
|
||||
.HasMaxLength(100)
|
||||
.IsUnicode(false);
|
||||
entity.Property(e => e.PostalCode)
|
||||
.HasMaxLength(20)
|
||||
.IsUnicode(false);
|
||||
|
||||
entity.HasOne(d => d.CountryCodeNavigation).WithMany(p => p.People)
|
||||
.HasForeignKey(d => d.CountryCode)
|
||||
.OnDelete(DeleteBehavior.ClientSetNull)
|
||||
.HasConstraintName("FK_Person_Country");
|
||||
});
|
||||
|
||||
modelBuilder.Entity<Registration>(entity =>
|
||||
{
|
||||
entity.ToTable("Registration");
|
||||
|
||||
entity.HasIndex(e => new { e.PersonId, e.SportId, e.EventId }, "UQ_Registration_PersonId_SportId_EventId").IsUnique();
|
||||
|
||||
entity.Property(e => e.RegisteredAt).HasDefaultValueSql("(sysdatetime())", "DF_Registration_RegisteredAt");
|
||||
|
||||
entity.HasOne(d => d.Event).WithMany(p => p.Registrations)
|
||||
.HasForeignKey(d => d.EventId)
|
||||
.HasConstraintName("FK_Registration_Event");
|
||||
|
||||
entity.HasOne(d => d.Person).WithMany(p => p.Registrations)
|
||||
.HasForeignKey(d => d.PersonId)
|
||||
.HasConstraintName("FK_Registration_Person");
|
||||
|
||||
entity.HasOne(d => d.Sport).WithMany(p => p.Registrations)
|
||||
.HasForeignKey(d => d.SportId)
|
||||
.HasConstraintName("FK_Registration_Sport");
|
||||
});
|
||||
|
||||
modelBuilder.Entity<Sport>(entity =>
|
||||
{
|
||||
entity.ToTable("Sport");
|
||||
|
||||
entity.HasIndex(e => e.Name, "UQ_Sport_Name").IsUnique();
|
||||
|
||||
entity.Property(e => e.Name)
|
||||
.HasMaxLength(100)
|
||||
.IsUnicode(false);
|
||||
});
|
||||
|
||||
OnModelCreatingPartial(modelBuilder);
|
||||
}
|
||||
|
||||
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
|
||||
}
|
||||
Reference in New Issue
Block a user