You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
164 lines
7.8 KiB
164 lines
7.8 KiB
using Entity_Framework.Entity;
|
|
using Entity_Framework.Entity.Relations;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System.Collections.Generic;
|
|
using System.Reflection.Emit;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace Entity_Framework
|
|
{
|
|
public class BDDContext : DbContext
|
|
{
|
|
// Constructeur
|
|
public BDDContext() { }
|
|
public BDDContext(DbContextOptions<BDDContext> option) : base(option) { }
|
|
|
|
|
|
// Fluent Api
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
base.OnModelCreating(modelBuilder);
|
|
|
|
// -------------------------------------------------------------------//
|
|
// Tables Pilotes
|
|
modelBuilder.Entity<Pilotes>().HasKey(e => e.Id);
|
|
modelBuilder.Entity<Pilotes>().Property(e => e.Id)
|
|
.ValueGeneratedOnAdd();
|
|
modelBuilder.Entity<Pilotes>().Property(e => e.Pseudo).IsRequired();
|
|
modelBuilder.Entity<Pilotes>().HasIndex(e => e.Pseudo).IsUnique();
|
|
modelBuilder.Entity<Pilotes>().Property(e => e.Email).IsRequired();
|
|
modelBuilder.Entity<Pilotes>().HasIndex(e => e.Email).IsUnique();
|
|
modelBuilder.Entity<Pilotes>().Property(e => e.Password).IsRequired();
|
|
|
|
//Relation avec écurie pour le propriétaire
|
|
modelBuilder.Entity<Pilotes>().HasOne(e => e.EcuriePropriétaire)
|
|
.WithOne(e => e.PiloteProprietaire)
|
|
.HasForeignKey<Ecuries>(e => e.IdPiloteProprietaire);
|
|
|
|
// Relation avec Session
|
|
modelBuilder.Entity<Pilotes>().HasMany(e => e.Sessions)
|
|
.WithOne(e => e.Pilote)
|
|
.HasForeignKey(e => e.IdPilote);
|
|
|
|
|
|
// -------------------------------------------------------------------//
|
|
// Tables Ecuries
|
|
modelBuilder.Entity<Ecuries>().HasKey(e => e.Id);
|
|
modelBuilder.Entity<Ecuries>().Property(e => e.Id)
|
|
.ValueGeneratedOnAdd();
|
|
modelBuilder.Entity<Ecuries>().Property(e => e.Name).IsRequired();
|
|
modelBuilder.Entity<Ecuries>().HasIndex(e => e.Name).IsUnique();
|
|
|
|
|
|
// Relation avec pilotes
|
|
/*
|
|
modelBuilder.Entity<Ecuries>().HasMany(e => e.Pilotes)
|
|
.WithOne(e => e.Ecurie)
|
|
.HasForeignKey(e => e.IdEcurie)
|
|
.IsRequired(false);*/
|
|
// -------------------------------------------------------------------//
|
|
// Tables Circuits
|
|
|
|
modelBuilder.Entity<Circuits>().HasKey(e => e.Id);
|
|
modelBuilder.Entity<Circuits>().Property(e => e.Id)
|
|
.ValueGeneratedOnAdd();
|
|
modelBuilder.Entity<Circuits>().Property(e => e.Name).IsRequired();
|
|
modelBuilder.Entity<Circuits>().HasIndex(e => e.Name).IsUnique();
|
|
|
|
modelBuilder.Entity<Circuits>().HasMany(e => e.Course)
|
|
.WithOne(e => e.Circuit)
|
|
.HasForeignKey(e => e.IdCircuit);
|
|
|
|
// -------------------------------------------------------------------//
|
|
// Tables Session
|
|
modelBuilder.Entity<Sessions>().HasKey(e => e.Id);
|
|
modelBuilder.Entity<Sessions>().Property(e => e.Id)
|
|
.ValueGeneratedOnAdd();
|
|
modelBuilder.Entity<Sessions>().Property(e => e.Name).IsRequired();
|
|
|
|
|
|
modelBuilder.Entity<Sessions>().HasMany(e => e.Tours)
|
|
.WithOne(e => e.Session)
|
|
.HasForeignKey(e => e.IdSession);
|
|
|
|
// -------------------------------------------------------------------//
|
|
// Tables Geolocalisations
|
|
/*
|
|
modelBuilder.Entity<Geolocalisations>().HasKey(e => e.Id);
|
|
modelBuilder.Entity<Geolocalisations>().Property(e => e.Id)
|
|
.ValueGeneratedOnAdd();
|
|
modelBuilder.Entity<Geolocalisations>().Property(e => e.Latitude).IsRequired();
|
|
modelBuilder.Entity<Geolocalisations>().Property(e => e.Longitude).IsRequired();
|
|
|
|
// Relation avec Points
|
|
modelBuilder.Entity<Geolocalisations>().HasOne(e => e.Point)
|
|
.WithOne(z => z.Geolocalisation)
|
|
.HasForeignKey<Points>(x => x.IdGeolocalisation);
|
|
*/
|
|
// -------------------------------------------------------------------//
|
|
// Tables Points
|
|
modelBuilder.Entity<Points>().HasKey(e => e.Id);
|
|
modelBuilder.Entity<Points>().Property(e => e.Id)
|
|
.ValueGeneratedOnAdd();
|
|
|
|
// -------------------------------------------------------------------//
|
|
// Tables Tours
|
|
|
|
modelBuilder.Entity<Tours>().HasKey(e => e.Id);
|
|
modelBuilder.Entity<Tours>().Property(e => e.Id)
|
|
.ValueGeneratedOnAdd();
|
|
|
|
modelBuilder.Entity<Tours>().HasMany(e => e.Points)
|
|
.WithOne(e => e.Tours)
|
|
.HasForeignKey(x => x.IdTours);
|
|
|
|
// -------------------------------------------------------------------//
|
|
// Tables Images
|
|
modelBuilder.Entity<Images>().HasKey(e => e.Id);
|
|
modelBuilder.Entity<Images>().Property(e => e.Id)
|
|
.ValueGeneratedOnAdd();
|
|
|
|
// -------------------------------------------------------------------//
|
|
// Tables Relation_Ecurie_Pilote
|
|
modelBuilder.Entity<Relation_Pilote_Ecurie>().HasKey(e => new { e.IdPilote, e.IdEcurie });
|
|
|
|
|
|
|
|
// ------------------------ STUB ------------------------------------ //
|
|
|
|
//stub data
|
|
byte[] passwordByte = Encoding.UTF8.GetBytes("test123");
|
|
SHA256 sha256 = SHA256.Create();
|
|
byte[] hashedByte = sha256.ComputeHash(passwordByte);
|
|
string hashedPassword = Convert.ToBase64String(hashedByte);
|
|
|
|
|
|
var pilote = new Pilotes { Id = 1, Pseudo = "test_PILOTE", Email = "test@gmail.com", Password = hashedPassword };
|
|
modelBuilder.Entity<Pilotes>().HasData(pilote);
|
|
modelBuilder.Entity<Circuits>().HasData(new Circuits { Id = 1, Name = "test_CIRCUIT" });
|
|
}
|
|
|
|
// DbSet
|
|
public DbSet<Pilotes> Pilotes { get; set; }
|
|
public DbSet<Circuits> Circuits { get; set; }
|
|
public DbSet<Sessions> Sessions { get; set; }
|
|
public DbSet<Ecuries> Ecuries { get; set; }
|
|
//public DbSet<Geolocalisations> Geolocalisations { get; set; }
|
|
public DbSet<Points> Points { get; set; }
|
|
public DbSet<Tours> Tours { get; set; }
|
|
public DbSet<Images> Images { get; set; }
|
|
public DbSet<Relation_Pilote_Ecurie> RelationEcuriePilote { get; set; }
|
|
|
|
// BDD
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
{
|
|
if (!optionsBuilder.IsConfigured)
|
|
{
|
|
optionsBuilder.UseSqlite($"Data Source=..\\Entity_Framework\\BDD.db");
|
|
// optionsBuilder.UseSqlite($"Data Source=C:\\Users\\Jolys Enzo\\home\\BUT\\Projet\\Sae4.01\\Projet\\R-Dash_APP\\Entity_Framework\\BDD.db");
|
|
//optionsBuilder.UseSqlite($"Data Source=C:\\Users\\zinn1\\Source\\Repos\\Serveur-Api2\\Sources\\Entity_Framework\\BDD.db");
|
|
}
|
|
}
|
|
}
|
|
} |