how to fix the error 'relation "table" does not exist' while creating a db using docker in .net

48 views Asked by At

I've trying to create a Docker image postgresql's Db for a simple app which consiste in school's subject with Students and Teachers. But when I get a simple GET request with Swagger, it throws Npgsql.PostgresException (0x80004005): 42P01: relation "Subjects" does not exist

This is my models:

`

public class Student
 {
    [Key]
    public int? Dni
    {get; set;}
    public string? FirstName { get; set; }
    public string? SurName { get; set; }
    public string? Email {get; set; }
    public string? Password {get; set;}
    public DateTime EnrollmentDate { get; set; }
    public ICollection<SubjectStudents> Subjects;

 }
 public class Teacher 
 {
    [Key]
    public int? Dni {get; set; }
    public string? FirstName {get; set; }
    public string? SurName {get; set; }
    public string? Email {get; set; }
    public string? Password {get; set;}
    public bool? Status {get; set; }

    public ICollection<Subject> Subjects {get; set;}

 }

 public class Subject
 {
    public string? id {get;set;} 
    public string? Title {get; set; }
    public int? Credits {get; set; }
    
   
    public virtual List<SubjectStudents> SubjectStudents {get; set;}

    [ForeignKey("Teacher")]         
    public int? TeacherDni {get; set;}
    public virtual Teacher Teacher {get; set;}          

  }

 [PrimaryKey(nameof(SubjectId), nameof(StudentDni))]
 public class SubjectStudent
 {
    public string? SubjectId {get;set;}
    public Subject Subjects {get;set;}
    public int? StudentDni {get;set;}
    public virtual Student Students {get; set;}

  }`

I tried to set a one-to-Many relationship with Teacher-Subject because a teacher can teach many subjects, while a Subject merely have a single teacher. I set the Teacher's Dni as foreign and primary key, as you can see below

In the second relationship, I settled Many-Many relationship to Student-Subject, because a Student can be in many subjects and Subject containe many students. To accomplish this goal, I created new joining entity class which includes the foreign key properties and the reference navigation properties for each entity(in this case Subject and Student).

0

There are 0 answers