Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LINQ query not working properly for many-to-many relationship entities in EF core #22801

Closed
csnarain opened this issue Sep 28, 2020 · 3 comments

Comments

@csnarain
Copy link

csnarain commented Sep 28, 2020

I have two entities in my database schema which has many-to-many relationship. I followed the EF core documentation (https://docs.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key) by setting up a bridge table and mentioning the foreign key in DbContext appropriately. However, when I try to query my db using the LINQ query (see below), it's simply returning only the parent object and returning null for my child objects. I've setup a navigation property appropriately.

_context.Activities.Where(a => a.Id == id)
                .Include(a => a.Groups)
                .ThenInclude(b => b.GroupMembers)
                .ThenInclude(p => p.Members)
                .ToListAsync();

Also configured the composite keys using fluent api as shown below:

         modelBuilder.Entity<StudentCourse>().HasKey(p => new { p.StudentId, p.CourseId});

I tried to parse the underlying SQL query using SQL studio profiler which is executing the correct sql queries and the results are also returned accurately. It's only when the results are returned back, it's not getting displayed properly. My class structure is as given below. Group and Member are the entities that have many-to-many relationship and GroupMember is my bridge entity class. Is this an issue or am I doing something wrong here.

public class Activity
{
    public int Id { get; set; }
    public string ActivityName { get; set; }
    public DateTime ActivityDate { get; set; }
    
    [ForeignKey("Event")]
    public int EventId { get; set; }
    
   
    //navigation property
    public List<Group> Groups{ get; set; } = new List<Groups>();
}
public class Group
{
    public int Id { get; set; }
    [ForeignKey("Activity")]
    public int ActivityId { get; set; }
    
    public string GroupName { get; set; }
    
    //navigation property
    public List<GroupMember> GroupMembers { get; set; } = new List<GroupMember>();

}
public class GroupMember
{
    public int GroupId { get; set; }
    public int MemberId { get; set; }

    public Group Groups { get; set; } = new Group();
    public Member Members { get; set; } = new Member();
}

public class Member
{
    public int Id { get; set; }
    public string DisplayName { get; set; }
    public string Email { get; set; }
    
    [ForeignKey("Event")]
    public int EventId { get; set; }
    
    public bool IsDeleted { get; set; }
    
    //navigation property
    public List<GroupMember> GroupMembers { get; set; } = new List<GroupMember>();
}
@smitpatel
Copy link
Member

Please share a runnable repro code which demonstrate issue you are seeing.

@csnarain
Copy link
Author

I'm not allowed to share my source files. However, whatever I've provided above is very similar to what I'm using in my modules. All I'd like to know is if there is anything I'm missing wrt configuring m2m relationship in efcore

@ajcvickers
Copy link
Member

ajcvickers commented Sep 30, 2020

@csnarain You cannot initialize reference navigation properties to empty objects. So this:

    public Group Groups { get; set; } = new Group();
    public Member Members { get; set; } = new Member();

Should be

    public Group Groups { get; set; }
    public Member Members { get; set; }

See #18007 for more info.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants