[英]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.