Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions exercise.wwwapi/Data/DataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
protected override void OnModelCreating(ModelBuilder modelBuilder)
{


modelBuilder.Entity<UserCohort>()
.HasKey(uc => new { uc.UserId, uc.CohortId });


modelBuilder.Entity<User>()
.Property(u => u.Role)
.HasConversion<string>();
Expand Down Expand Up @@ -49,5 +54,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
public DbSet<User> Users { get; set; }
public DbSet<Post> Posts { get; set; }
public DbSet<PostComment> PostComments { get; set; }
public DbSet<Cohort> Cohorts { get; set; }
public DbSet<UserCohort> UserCohorts { get; set; }
}
}
14 changes: 14 additions & 0 deletions exercise.wwwapi/Models/Cohort.cs
Original file line number Diff line number Diff line change
@@ -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 int Id { get; set; }
[Column("title")]
public string Title { get; set; } = string.Empty;
}
}
20 changes: 20 additions & 0 deletions exercise.wwwapi/Models/UserCohort.cs
Original file line number Diff line number Diff line change
@@ -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; }
Comment thread
Vegard-S marked this conversation as resolved.
[ForeignKey("CohortId")]
public Cohort? Cohort { get; set; }


[Column("userid")]
public required int UserId { get; set; }
[ForeignKey("UserId")]
public User? User { get; set; }
}
}
Loading