T
T
TeVeTed2016-06-12 15:55:16
ASP.NET
TeVeTed, 2016-06-12 15:55:16

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; }
    }

And an intermediate model that implements a many-to-many relationship:
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; }
    }

I'm sure I screwed up in the intermediate model, but maybe I screwed up in the binders
Help, who knows)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sanostee, 2016-06-12
@andrewpianykh

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; }
    }

Read more: www.entityframeworktutorial.net/code-first/configu...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question