2016-12-03 1 views
2

Ich habe zwei Modelle und ein Modell, um die beiden zu verbinden.ASP.NET-Kern mit Entity Framework viele-zu-viele funktioniert nicht

Organization Modell:

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 Modell:

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

Nach einigen Recherchen ich diesen Code auf die OnModelCreating Methode ebenfalls hinzugefügt.

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

Mit dem Debugger ich, dass OrganizationApplicationUsers herausgefunden bleibt null.

Was könnte ich tun, um dies zu beheben?

+0

Ihre Namensgebung etwas verwirrend ist. meinst du 'Organization.OrganizationApplicationUsers' oder' ApplicationDbContext.OrganizationApplicationUsers' bleibt null? –

+0

* Wenn * bleibt es "null"? Wenn Sie erwarten, dass es zu faul lädt - das wird (noch) nicht unterstützt. –

Antwort

0

Wenn Sie wollen, was eine viele zu viele Beziehung zwischen Entitäten genannt wird, müssen Sie die ApplicationUser direkt mit der Organization verknüpfen (unter der Haube, Entity Framework wird die Tabelle zwischen erstellen, aber Ihre Klasse wird nicht darüber weiß.

Andere als das, in den ApplicationUser, soll es ein IColletion sein und nicht ein IQueryable.

Verwandte Themen