Yazan Rawashdeh
Yazan Rawashdeh

Reputation: 1041

how to query against a many to many relation with entity framework 6

I have those 2 Models

public class BranchEmployees
{
    public int ID { get; set; }

    [Required, Column(Order = 0), Key]
    public string ApplicationUserID { get; set; }

    [Required, Column(Order = 1), Key]
    public int BranchID { get; set; } 

    public virtual ICollection<ApplicationUser> ApplicationUser { get; set; }

    public virtual ICollection<Branch> Branch { get; set; }
}



public class Branch
{
    public int ID { get; set; }

    public string BranchName { get; set; }

    [Required]
    public string ApplicationUserID { get; set; }

    public ApplicationUser User { get; set; }

    public virtual ICollection<BranchEmployees> BranchEmployees { get; set; }
}

public class ApplicationUser
{
    //rest of the code
}

UPDATE I have everything set up but what I want is the query that gets me the Employees whose IDs are in the branch employees table , I'm using entity framework code first with MVC 5 , how do I do it ?

Upvotes: 0

Views: 49

Answers (2)

renakre
renakre

Reputation: 8291

Assuming that your ApplicationUser class will have a navigational property called BranchEmployees, here is the query that gets me the Employees whose IDs are in the branch employees table

 List<ApplicationUsers> employeeNames = 
               dbContext
               .ApplicationUsers
               .Where(au => au.BranchEmployees
                              .Count() > 0).ToList();

Also, can you provide whole model including ApplicationUser? I also wonder why you do not prefer BranchEmployees to inherit from ApplicationUser.

Upvotes: 1

Martin Shishkov
Martin Shishkov

Reputation: 2837

You don't need a class that indicates a many-to-many relation between two tables when you do code-first. The key here is to create virtual properties of those classes. Lets say you have a class Student and class Course. Students can be in many Courses and Courses can have many Students. To generate a database using these models the classes should look like this:

public class Student
    {
        private ICollection<Course> _courses; 

        public Student()
        {
            this._courses = new HashSet<Course>();
        }

        [Key]
        public int Id { get; set; }

        public string FullName { get; set; }

        public virtual ICollection<Course> Courses
        {
            get { return this._courses; }
            set { this._courses = value; }
        } 
    }

And for Course:

public class Course
{
    private ICollection<Student> _students; 

    public Course()
    {
        this._students = new HashSet<Student>();
    }

    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }

    public virtual ICollection<Student> Students
    {
        get { return this._students; }
        set { this._students = value; }
    } 
}

I hope that this can help you solve your issue.

Upvotes: 0

Related Questions