ASP.NET核心与实体框架多对多无法正常工作

时间:2022-10-05 21:13:07

I have two models, and a model to connect the two.

我有两个模型,一个模型连接两个。

Organization model:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Verbonding.Models
{
    public class Organization
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public ICollection<OrganizationApplicationUser> OrganizationApplicationUsers { get; set; }

        public Organization()
        {
            IsActive = true;
            IsBlocked = false;
            DateCreated = DateTime.Now;
            DateUpdated = DateTime.Now;
        }

        public ApplicationUser AddUser(ApplicationUser user)
        {
            OrganizationApplicationUser ou = new OrganizationApplicationUser { ApplicationUser = user, Organization = this };

            OrganizationApplicationUsers.Add(ou);
            return user;
        }
    }
}

ApplicationUser model:

using System;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using System.Linq;

namespace Verbonding.Models
{
    public class ApplicationUser : IdentityUser
    {
        public bool IsActive { get; set; }

        public IQueryable<OrganizationApplicationUser> OrganizationApplicationUsers { get; set; }

        public ApplicationUser():base()
        {
            IsActive = true;
            IsBlocked = false;
            DateJoined = DateTime.Now;
            DateUpdated = DateTime.Now;
        }

        public IQueryable<Organization> GetOrganizations()
        {
            return OrganizationApplicationUsers.Select(x => x.Organization);
;       }
    }
}

DbContext:

using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Verbonding.Models;

namespace Verbonding.Data
{
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            builder.Entity<OrganizationApplicationUser>().HasKey(x => new { x.OrganizationId, x.ApplicationUserId });
        }

        public DbSet<Country> Countries { get; set; }
        public DbSet<Address> Addresses { get; set; }
        public DbSet<Organization> Organizations { get; set; }
        public DbSet<Category> Categories { get; set; }
        public DbSet<Event> Events { get; set; }
        public DbSet<OrganizationApplicationUser> OrganizationApplicationUsers { get; set; }
    }
}

After some research I also added this code to the OnModelCreating method.

经过一些研究后,我还将此代码添加到OnModelCreating方法中。

builder.Entity<OrganizationApplicationUser>()
    .HasOne(ou => ou.Organization)
    .WithMany(o => o.OrganizationApplicationUsers)
    .HasForeignKey(ou => ou.Organization);

builder.Entity<OrganizationApplicationUser>()
    .HasOne(ou => ou.ApplicationUser)
    .WithMany(u => u.OrganizationApplicationUsers)
    .HasForeignKey(ou => ou.ApplicationUserId);

Using the debugger I found out that OrganizationApplicationUsersremains null.

使用调试器我发现OrganizationApplicationUsersremains为null。

What could I do to fix this?

我该怎么做才能解决这个问题?

1 个解决方案

#1


0  

if you want to have what is called a many to many relationship between entities, you need to link the ApplicationUser to the Organization directly (under the hood, entity framework is gonna create the table between, but your class won't know about it.

如果你想拥有实体之间所谓的多对多关系,你需要直接将ApplicationUser链接到组织(实体框架之间会创建表格,但你的班级不会知道它。

Other than that, in the ApplicationUser, it should be an IColletion and not an IQueryable.

除此之外,在ApplicationUser中,它应该是IColletion而不是IQueryable。

#1


0  

if you want to have what is called a many to many relationship between entities, you need to link the ApplicationUser to the Organization directly (under the hood, entity framework is gonna create the table between, but your class won't know about it.

如果你想拥有实体之间所谓的多对多关系,你需要直接将ApplicationUser链接到组织(实体框架之间会创建表格,但你的班级不会知道它。

Other than that, in the ApplicationUser, it should be an IColletion and not an IQueryable.

除此之外,在ApplicationUser中,它应该是IColletion而不是IQueryable。