简体   繁体   English

带有实体框架的ASP.NET核心多对多不起作用

[英]ASP.NET core with Entity Framework many-to-many not working

I have two models, and a model to connect the two. 我有两个模型,还有一个将两者连接的模型。

Organization model: Organization模式:

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: ApplicationUser模型:

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 : 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 OrganizationApplicationUsers remains null . 使用调试器,我发现OrganizationApplicationUsers保持为null

What could I do to fix this? 我该怎么做才能解决此问题?

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链接到Organization (在幕后,实体框架将在它们之间创建表,但是您的班级对此一无所知。

Other than that, in the ApplicationUser , it should be an IColletion and not an IQueryable . 除此之外,在ApplicationUser ,它应该是IColletion而不是IQueryable

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET Core 5 Blazor WASM、gRPC、Entity Framework Core 5:多对多导致堆栈溢出 - ASP.NET Core 5 Blazor WASM, gRPC, Entity Framework Core 5: many-to-many results in stack overflow Entity Framework Core 5.0 如何将 LINQ 转换为多对多连接以使用 ASP.NET 成员资格的交集表 - Entity Framework Core 5.0 How to convert LINQ for many-to-many join to use Intersection table for ASP.NET Membership ASP.Net实体框架多对多更新无法正常工作 - ASP.Net Entity Framework many to many update not working ASP .NET Core MVC Entity Framework - 如何访问由多对多关系产生的表? - ASP .NET Core MVC Entity Framework - How to get access to a table which is result of many-to-many relationship? ASP .NET 核心 MVC 实体框架 - 将数据添加到多对多关系中 - ASP .NET Core MVC Entity Framework - add data into many-to-many relationship 使用ASP.NET Core MVC MultiSelectList更新多对多实体 - Update many-to-many entity with ASP.NET Core MVC MultiSelectList ASP.NET, Entity Framework Code First 多对多自引用列表 - ASP.NET, Entity Framework Code First many-to-many self referencing lists 如何通过实体框架访问多对多表? asp.net - How to access many-to-many table via Entity Framework? asp.net ASP.NET MVC实体框架使用MultiSelectList插入到多对多表 - ASP.NET MVC Entity Framework insert to many-to-many table using MultiSelectList 更新 asp.net 核心 mvc 中的多对多关系 - Updating a many-to-many relationship in asp.net core mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM