具有实体框架的ASP.NET核心多对多无效

时间:2016-12-03 17:57:48

标签: c# asp.net entity-framework

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

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型号:

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; }
    }
}

经过一些研究后,我还将此代码添加到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);

使用调试器我发现OrganizationApplicationUsers仍为null

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

1 个答案:

答案 0 :(得分:0)

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

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