Question

I am using Code First LINQ To SQL Approach and trying to retrieve records from two tables by using junction table. I have structured data model in very simple way and include Foriegn Keys in the child classes as properties like "CourseId"( Course Id was primary key in Program Class).

So My many to Many Relationship is somehow structured in classes like :

Program (ProgramId,ProgramName) Course (CourseId,CourseName,CreditPts) ProgramCourse ( ProgramId, CourseId ) <-- Junction Table

Here's my data model.

public class ProgramCourse
{        
    [Required]
    public int ProgramId { get; set; }

    [Required]
    public int CourseId { get; set; }                        
}

public class Program
{
    public int ProgramId { get; set; }

    [Required]
    [Display(Name = "Program Name")]
    public string ProgramName { get; set; }

    public int InstituteId { get; set; }

    // public virtual Institute Institute { get; set; }

}        
class Course
{
    public int CourseID { get; set; }

    [Required]
    [Display(Name = "Course Name")]
    public string CourseName { get; set; }

    [Required]
    [Display(Name = "Credit Points")]
    public string CreditPoints { set; get; }

}

My Problem

I am not able to retrieve the data from both tables ( i.e Program and Course ). I want to write method "GetAllCoursesForProgram" which displays the courses for specific program. Because I can't navigate towards "ProgramCourse" class from Course class or from Program class. Because foreign keys included as simple int's and there are no navigational properties included in the whole data model. If I include navigational properties , I would have problems to manipulate these object and making AJAX based calls to server from jTable.

Any body can suggest me the solution ? How to form a query to retrieve data from multiple tables ? I would be extremely obliged. Many Thanks in advance.

Regards Usman

Était-ce utile?

La solution

If you are using Code First for a many to many relationship, DO NOT CREATE THE JUNCTION TABLE YOURSELF! Code First will do it for you, and you will get Navigation properties, solving your problem.

Instead of having problems manipulating those objects, it will solve your problem.....

Start by changing your model, generate the database, look how Code First created the 3rd "junction" table, then see how to work with that in code. Let me know if it does not help.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top