C# 自定义模型和AspNetUsers之间的一对多关系

C# 自定义模型和AspNetUsers之间的一对多关系,c#,asp.net-core,entity-framework-core,C#,Asp.net Core,Entity Framework Core,我想在自定义表(称为Organization)和AspNetUsers 每个用户只能属于一个组织 每个组织可以有许多用户 我希望组织表中有一列包含属于该组织的每个用户。 对于任何组织,我需要此-->的原因是我需要能够从该组织检索所有用户 组织模型具有属性-ICollection of ApplicationUser: public class Organization { public int Id { get; set; } public str

我想在自定义表(称为
Organization
)和
AspNetUsers

  • 每个用户只能属于一个组织
  • 每个组织可以有许多用户
我希望
组织
表中有一列包含属于该组织的每个用户。

对于任何
组织,我需要此-->的原因是我需要能够从该组织检索所有用户

组织
模型具有属性-ICollection of ApplicationUser:

public class Organization
    {
        public int Id { get; set; }
        public string Name { get; set; }
        
        public ICollection<ApplicationUser> ApplicationUsers { get; set; }
    }
在添加迁移和更新数据库的这一阶段:

  • 我在
    AspNetUsers
    表中获得了一列关于组织的信息:
    Organization\u Id
  • 但是我没有在
    组织
    表中获得用户列表的列
这是我的
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);
            });

公共类AppDbContext:IdentityDbContext
{
公共AppDbContext(DbContextOptions选项)
:基本(选项)
{
}
公共数据库集组织{get;set;}
模型创建时受保护的覆盖无效(ModelBuilder)
{
基于模型创建(生成器);
builder.Entity(b=>
{
b、 HasKey(p=>p.Id);
b、 有许多(p=>p.ApplicationUsers)
.有一个(p=>p.Organization);
});
提前谢谢你的帮助

我在AspNetUsers表中得到一列Organization:Organization\u Id 但是我没有在Organization表中获得用户列表的列

这是在EF core中创建一对多关系时的默认约定,在配置了这些关系之后,我们可以通过navigation属性找到相关实体

有关详细信息,请查看以下教程:

对于任何组织,我都需要能够从中检索所有用户 该组织

要加载相关实体,可以使用
Include
方法,检查action方法中的linq语句:

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();
    }
调试屏幕截图如下所示:


更多详细信息,请选中和

“但我没有在组织表中获得用户列表的列”-这是预期的。如何将用户列表存储在关系数据库的列中?一对多关系通常是这样工作的,您不需要任何类似的东西。根据您的配置,
Organization。每当您使用EF查询时,ApplicationUsers
将是属于该组织的所有用户的集合。示例:
来自ctx.Organizations where org.ApplicationUsers.Any()选择org
@AluanHaddad谢谢,我现在明白了。感谢您的详细解释。谢谢!
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();
    }