ZAD
ZAD

Reputation: 51

One to Many relationship between Custom Model and AspNetUsers

I want to create a one to many relationship between a custom table (called Organization) and AspNetUsers

I would like the Organization table to have a column containing each User belonging to that organization.

The reason I need this --> for any Organization I need to be able to retrieve all the Users from that Organization.

The Organization model has a property - ICollection of ApplicationUser:

public class Organization
    {
        public int Id { get; set; }
        public string Name { get; set; }
        
        public ICollection<ApplicationUser> ApplicationUsers { get; set; }
    }

In the ApplicationUser model:

    public class ApplicationUser : IdentityUser
    {
        public Organization Organization { get; set; }
    }

At this stage when I add a migration and update database:

This is my AppDbContext:

public class AppDbContext : IdentityDbContext<ApplicationUser>
    {
        public AppDbContext(DbContextOptions<AppDbContext> options)
            : base(options)
        {
 
        }
        public DbSet<Orgnization> Organization{ get; set; }
 
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
 
            builder.Entity<Organization>(b =>
            {
                b.HasKey(p => p.Id);
 
                b.HasMany(p => p.ApplicationUsers)
                .WithOne(p => p.Organization);
            });

Thanks in advance for any help

Upvotes: 0

Views: 860

Answers (1)

Zhi Lv
Zhi Lv

Reputation: 21383

I get a column for Organization in AspNetUsers table: Organization_Id But I do not get a column for list of Users in the Organization table

This is the default convention when create One-to-Many relationships in EF core, after configure the relationships between then, we could find the related entities via the navigation property.

More detail information, check the following tutorials:

One-to-Many Relationship Conventions in Entity Framework Core

Configuring One To Many Relationships in Entity Framework Core

for any Organization I need to be able to retrieve all the Users from that Organization.

To load the related entities, you could use the Include method, check the linq statement in the action method:

public class HomeController : Controller
{ 
    private readonly ApplicationDbContext _context; 
    public HomeController(ApplicationDbContext context)
    { 
        _context = context;
    }
     

    public IActionResult Index()
    {
        if (User.Identity.IsAuthenticated)
        {
            //Based on user's name to get the organization.
            var organization = _context.Organization.Include(c=>c.ApplicationUsers).Where(c => c.ApplicationUsers.Any(d => d.UserName == User.Identity.Name)).ToList();
              
            //require using Microsoft.EntityFrameworkCore;
            //based on the organization name, find all users belong to the specific organization.
            var result = _context.Organization.Include(c => c.ApplicationUsers).Where(c => c.Name == "microsoft")
                .SelectMany(c => c.ApplicationUsers.Select(d => new { UserName = d.UserName, OrganizationName = c.Name })).ToList();

        }
        return View();
    }

The debug screenshot as below:

enter image description here

More detail information, check Loading Related Data and Tutorial: Read related data - ASP.NET MVC with EF Core

Upvotes: 1

Related Questions