Answer the question
In order to leave comments, you need to log in
Why is the many-to-many relationship not working?
I made 2 models of tables Colleges and Specialties, between which there should be a many-to-many relationship (as if a technical school teaches in several specialties, and some specialty can be studied in several technical schools)
Here are the models:
public class College
{
public int CollegeId { get; set; }
public string Name { get; set; }
public string Locality { get; set; }
public int LocalityTypeId { get; set; }
public virtual LocalityType LocalityType { get; set; }
public int? UniversityId { get; set; }
public virtual University University { get; set; }
public int AreaId { get; set; }
public virtual Area Area { get; set; }
public string Address { get; set; }
public string Phone { get; set; }
public string Site { get; set; }
public string Email { get; set; }
public int? StatusId { get; set; }
public virtual Status Status { get; set; }
public int? AccrLevel { get; set; }
public Director DirectorId { get; set; }
public bool? Full { get; set; }
public bool? Demo { get; set; }
public List<Speciality> Specialities { get; set; }
}
public class Speciality
{
public string DirectionCode { get; set; }
public virtual Direction Direction { get; set; }
[Key]
public string SpecialityCode { get; set; }
public string Name { get; set; }
public List <College> Colleges { get; set; }
}
public class CollegeSpeciality
{
public int CollegeSpecialityId { get; set; }
public int CollegeId { get; set; }
public virtual College College { get; set; }
public string SpecialityCode { get; set; }
public virtual Speciality Speciality { get; set; }
public List<Speciality> Specialities { get; set; }
public List<College> Colleges { get; set; }
}
Answer the question
In order to leave comments, you need to log in
In your case, you do not need to explicitly create a link table, it will be created automatically
public class College
{
public int? AccrLevel { get; set; }
public string Address { get; set; }
public virtual Area Area { get; set; }
public int AreaId { get; set; }
public bool? Demo { get; set; }
public virtual Director Director { get; set; }
public int DirectorId { get; set; }
public string Email { get; set; }
public bool? Full { get; set; }
[Key]
public int CollegeId { get; set; }
public string Locality { get; set; }
public virtual LocalityType LocalityType { get; set; }
public int LocalityTypeId { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
public string Site { get; set; }
public virtual ICollection<Speciality> Specialities { get; set; }
public virtual Status Status { get; set; }
public int? StatusId { get; set; }
public virtual University University { get; set; }
public int? UniversityId { get; set; }
}
public class Speciality
{
public virtual ICollection<College> Colleges { get; set; }
[ForeignKey("DirectionCode")]
public virtual Direction Direction { get; set; }
public string DirectionCode { get; set; }
public string Name { get; set; }
[Key]
public string SpecialityCode { get; set; }
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question