From a1e282951c285f92732836cd7c3ec250527f8324 Mon Sep 17 00:00:00 2001 From: Vegard Stigen Date: Thu, 11 Sep 2025 13:29:24 +0200 Subject: [PATCH 1/2] made models and added composite primary key to userCohort in dataContext --- exercise.wwwapi/Data/DataContext.cs | 7 +++++++ exercise.wwwapi/Models/Cohort.cs | 14 ++++++++++++++ exercise.wwwapi/Models/UserCohort.cs | 20 ++++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 exercise.wwwapi/Models/Cohort.cs create mode 100644 exercise.wwwapi/Models/UserCohort.cs diff --git a/exercise.wwwapi/Data/DataContext.cs b/exercise.wwwapi/Data/DataContext.cs index 8e35b7f..92c9151 100644 --- a/exercise.wwwapi/Data/DataContext.cs +++ b/exercise.wwwapi/Data/DataContext.cs @@ -20,6 +20,11 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder) { + + modelBuilder.Entity() + .HasKey(uc => new { uc.UserId, uc.CohortId }); + + modelBuilder.Entity() .Property(u => u.Role) .HasConversion(); @@ -49,5 +54,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) public DbSet Users { get; set; } public DbSet Posts { get; set; } public DbSet PostComments { get; set; } + public DbSet Cohorts { get; set; } + public DbSet UserCohorts { get; set; } } } diff --git a/exercise.wwwapi/Models/Cohort.cs b/exercise.wwwapi/Models/Cohort.cs new file mode 100644 index 0000000..e06a248 --- /dev/null +++ b/exercise.wwwapi/Models/Cohort.cs @@ -0,0 +1,14 @@ +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace exercise.wwwapi.Models +{ + [Table("cohorts")] + public class Cohort + { + [Key, Column("id")] + public required int Id { get; set; } + [Column("title")] + public string Title { get; set; } = string.Empty; + } +} diff --git a/exercise.wwwapi/Models/UserCohort.cs b/exercise.wwwapi/Models/UserCohort.cs new file mode 100644 index 0000000..0883024 --- /dev/null +++ b/exercise.wwwapi/Models/UserCohort.cs @@ -0,0 +1,20 @@ +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace exercise.wwwapi.Models +{ + [Table("userCohorts")] + public class UserCohort + { + [Column("cohortid")] + public required int CohortId { get; set; } + [ForeignKey("CohortId")] + public Cohort? Cohort { get; set; } + + + [Column("userid")] + public required int UserId { get; set; } + [ForeignKey("UserId")] + public User? User { get; set; } + } +} From efaa70209a86da51dba4fcad01b0efad6bcef55c Mon Sep 17 00:00:00 2001 From: Vegard Stigen Date: Thu, 11 Sep 2025 13:46:32 +0200 Subject: [PATCH 2/2] removed required in Cohort model --- exercise.wwwapi/Models/Cohort.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercise.wwwapi/Models/Cohort.cs b/exercise.wwwapi/Models/Cohort.cs index e06a248..3cfaa6a 100644 --- a/exercise.wwwapi/Models/Cohort.cs +++ b/exercise.wwwapi/Models/Cohort.cs @@ -7,7 +7,7 @@ namespace exercise.wwwapi.Models public class Cohort { [Key, Column("id")] - public required int Id { get; set; } + public int Id { get; set; } [Column("title")] public string Title { get; set; } = string.Empty; }