Home > front end >  Entity Framework Core Relationship Problem
Entity Framework Core Relationship Problem

Time:04-24

The 1-N relationship that I made when I wanted to create individual todos for the user is as follows.

public class TaskApp
{
    public string Topic { get; set; }

    public string Description { get; set; }

    public bool IsCompleted { get; set; }

    public string UserAppId { get; set; }

}

public class UserApp:IdentityUser
{
    public string NameSurname { get; set; }

    public ICollection<TaskApp> TaskApps { get; set; }
}

What's happening here is that I didn't show the User table in the TaskApp folder, and I just specified it as UserAppId.

When I want to add TaskApp, I also go to UserAppId and bring the claim name side of the log-in user.

How can I show this in my classes ?

CodePudding user response:

There is nothing wrong with having two-way navigation in EF Core, so I believe you can use the following entities:

public class TaskApp
{
    public string Topic { get; set; }

    public string Description { get; set; }

    public bool IsCompleted { get; set; }

    public string UserAppId { get; set; }

    // Adding this reference is not a bad practice
    public UserApp UserApp { get; set; }
}

public class UserApp : IdentityUser
{
    public string NameSurname { get; set; }

    public ICollection<TaskApp> TaskApps { get; set; }
}

Now you can use .Include and navigate as per below:

context.TaskApp.Include(x => x.UserApp);

Or if you need filter by any condition:

context.TaskApp.Include(x => x.UserApp.Where(q => q.IsCompleted == true);
  • Related