//----------------------------------------------------------------------- // FILENAME: HeartTrackContextLibraryContext.cs // PROJECT: DbContextLib // SOLUTION: FitnessApp // DATE CREATED: 22/02/2024 // AUTHOR: Antoine PEREDERII //----------------------------------------------------------------------- using Entities; using Microsoft.EntityFrameworkCore; namespace DbContextLib { /// /// Represents the database context for the FitnessApp. /// public class HeartTrackContext : DbContext { /// /// Gets or sets the set of athletes. /// public DbSet AthletesSet { get; set; } /// /// Gets or sets the set of activities. /// public DbSet ActivitiesSet { get; set; } /// /// Gets or sets the set of data sources. /// public DbSet DataSourcesSet { get; set; } /// /// Gets or sets the set of heart rates. /// public DbSet HeartRatesSet { get; set; } /// /// Gets or sets the set of notifications. /// public DbSet NotificationsSet { get; set; } /// /// Gets or sets the set of statistics. /// public DbSet StatisticsSet { get; set; } /// /// Gets or sets the set of trainings. /// public DbSet TrainingsSet { get; set; } /// /// Initializes a new instance of the class. /// public HeartTrackContext() : base() { } /// /// Initializes a new instance of the class with the specified options. /// /// The options for the context. public HeartTrackContext(DbContextOptions options) : base(options) { } public HeartTrackContext(string dbPlatformPath) : this(InitPlaformDb(dbPlatformPath)) { } private static DbContextOptions InitPlaformDb(string dbPlatformPath) { var options = new DbContextOptionsBuilder() .UseMySql($"{dbPlatformPath}", new MySqlServerVersion(new Version(10, 11, 1))) .Options; return options; } /// /// Configures the database options if they are not already configured. /// /// The options builder instance. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { Console.WriteLine("!IsConfigured..."); optionsBuilder.UseSqlite($"Data Source=uca_HeartTrack.db"); } } /// /// Configures the model for the library context. /// /// The model builder instance. protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity() .HasKey(a => a.IdActivity); //generation mode (at insertion) modelBuilder.Entity() .Property(a => a.IdActivity) .ValueGeneratedOnAdd(); //primary key of HeartRateEntity modelBuilder.Entity() .HasKey(h => h.IdHeartRate); //generation mode (at insertion) modelBuilder.Entity() .Property(h => h.IdHeartRate) .ValueGeneratedOnAdd(); //primary key of DataSourceEntity modelBuilder.Entity() .HasKey(d => d.IdSource); //generation mode (at insertion) modelBuilder.Entity() .Property(d => d.IdSource) .ValueGeneratedOnAdd(); //primary key of AthleteEntity modelBuilder.Entity() .HasKey(at => at.IdAthlete); //generation mode (at insertion) modelBuilder.Entity() .Property(at => at.IdAthlete) .ValueGeneratedOnAdd(); // add image column type // modelBuilder.Entity() // .Property(at => at.ProfilPicture) // .HasColumnType("image"); //primary key of StatisticEntity modelBuilder.Entity() .HasKey(s => s.IdStatistic); //generation mode (at insertion) modelBuilder.Entity() .Property(s => s.IdStatistic) .ValueGeneratedOnAdd(); //primary key of TrainingEntity modelBuilder.Entity() .HasKey(t => t.IdTraining); //generation mode (at insertion) modelBuilder.Entity() .Property(t => t.IdTraining) .ValueGeneratedOnAdd(); //primary key of NotificationEntity modelBuilder.Entity() .HasKey(n => n.IdNotif); //generation mode (at insertion) modelBuilder.Entity() .Property(n => n.IdNotif) .ValueGeneratedOnAdd(); modelBuilder.Entity() .HasKey(f => new { f.FollowingId, f.FollowerId }); modelBuilder.Entity() .HasOne(fing => fing.Following) .WithMany(fings => fings.Followings) .HasForeignKey(fing => fing.FollowingId); modelBuilder.Entity() .HasOne(fer => fer.Follower) .WithMany(fers => fers.Followers) .HasForeignKey(fing => fing.FollowerId); // ! // ? Plusieurs questions sur les required ou non, différence difficile à comprendre modelBuilder.Entity() .HasMany(at => at.TrainingsCoach) .WithOne(tc => tc.Coach) .HasForeignKey(tc => tc.CoachId); modelBuilder.Entity() .HasMany(at => at.TrainingsAthlete) .WithMany(ta => ta.Athletes); modelBuilder.Entity() .HasMany(at => at.NotificationsReceived) .WithMany(nr => nr.Receivers); modelBuilder.Entity() .HasMany(at => at.NotificationsSent) .WithOne(ns => ns.Sender) .HasForeignKey(ns => ns.SenderId); // required car on veut toujours savoir le receveur et l'envoyeur de la notification meme admin ou systeme modelBuilder.Entity() .HasMany(at => at.Statistics) .WithOne(s => s.Athlete) .HasForeignKey(s => s.AthleteId) .IsRequired(false); modelBuilder.Entity() .HasMany(at => at.Activities) .WithOne(a => a.Athlete) .HasForeignKey(a => a.AthleteId) .IsRequired(false); modelBuilder.Entity() .HasMany(a => a.HeartRates) .WithOne(h => h.Activity) .HasForeignKey(h => h.ActivityId) .IsRequired(); modelBuilder.Entity() .HasMany(d => d.Activities) .WithOne(a => a.DataSource) .HasForeignKey(a => a.DataSourceId) .IsRequired(); modelBuilder.Entity() .HasMany(ds => ds.Activities) .WithOne(at => at.DataSource) .HasForeignKey(at => at.DataSourceId) .IsRequired(false); // modelBuilder.Entity() // .HasMany(fer => fer.Followers) // .WithMany(fing => fing.Followings) // .UsingEntity( // l => l.HasOne().WithMany().HasForeignKey(fer => fer.FollowerId), // r => r.HasOne().WithMany().HasForeignKey(fing => fing.FollowingId), // j => j.Property(f => f.StartDate).HasDefaultValueSql("CURRENT_TIMESTAMP") // ); } } }