C# 使用costum对象从应用程序用户获取数据

C# 使用costum对象从应用程序用户获取数据,c#,asp.net-core,C#,Asp.net Core,我有一个与类别有多对多关系的应用程序用户。 我已尝试根据EntityFramework最新指南实现这种关系 [Table("AspNetUsers")] public class ApplicationUser: IdentityUser { public ApplicationUser() { ApplicationUserCategories = new HashSet<ApplicationUserCatego

我有一个与类别有多对多关系的应用程序用户。 我已尝试根据EntityFramework最新指南实现这种关系

[Table("AspNetUsers")]
    public class ApplicationUser: IdentityUser
    {

        public ApplicationUser()
        {
            ApplicationUserCategories = new HashSet<ApplicationUserCategory>();
        }

      public virtual ICollection<ApplicationUserCategory> ApplicationUserCategories { get; set; }


[Table("ApplicationUserCategories")]
    public class ApplicationUserCategory
    {
        [Key, Column(Order = 1)]
        public string ApplicationUserID { get; set; }
        [Key, Column(Order = 2)]
        public int CategoryID { get; set; }
        public ApplicationUser ApplicationUser { get; set; }
        public Category Category { get; set; }

    }

 [Table("Categories")]
    public class Category
    {

        public Category()
        {
            ApplicationUserCategories = new HashSet<ApplicationUserCategory>();
        }
      public int ID { get; set; }
        public string Name { get; set; }

        public virtual ICollection<ApplicationUserCategory> ApplicationUserCategories { get; set; }
    }


public class ApplicationContext : IdentityDbContext<ApplicationUser, IdentityRole, string>
    {
        public virtual DbSet<Category> Categories { get; set; }
        public virtual DbSet<ApplicationUserCategory> ApplicationUserCategories { get; set; }
        public ApplicationContext(DbContextOptions<ApplicationContext> options) : base(options)
        {

        }

        protected override void OnModelCreating(ModelBuilder builder)
        {

            base.OnModelCreating(builder);
            builder.Entity<ApplicationUserCategory>(
            build =>
            {

                build.HasKey(t => new { t.ApplicationUserID, t.CategoryID });
            });

        }
    }

您可以通过在
OnModelCreating
方法中添加以下代码来创建多对多关系:

protected override void OnModelCreating(ModelBuilder builder)
{

    base.OnModelCreating(builder);
    builder.Entity<ApplicationUserCategory>(
    build =>
    {

        build.HasKey(t => new { t.ApplicationUserID, t.CategoryID });
    });

    builder.Entity<ApplicationUserCategory>()
        .HasOne(pc => pc.ApplicationUser)
        .WithMany(p => p.ApplicationUserCategories)
        .HasForeignKey(pc => pc.ApplicationUserID);

    builder.Entity<ApplicationUserCategory>()
        .HasOne(pc => pc.Category)
        .WithMany(c => c.ApplicationUserCategories)
        .HasForeignKey(pc => pc.CategoryID);

}

如果您使用的是.Net core,则需要使用依赖项注入来注入DbContext,然后从控制器调用DbContext,例如:var categories=_context.GetCategories().toListSync();
protected override void OnModelCreating(ModelBuilder builder)
{

    base.OnModelCreating(builder);
    builder.Entity<ApplicationUserCategory>(
    build =>
    {

        build.HasKey(t => new { t.ApplicationUserID, t.CategoryID });
    });

    builder.Entity<ApplicationUserCategory>()
        .HasOne(pc => pc.ApplicationUser)
        .WithMany(p => p.ApplicationUserCategories)
        .HasForeignKey(pc => pc.ApplicationUserID);

    builder.Entity<ApplicationUserCategory>()
        .HasOne(pc => pc.Category)
        .WithMany(c => c.ApplicationUserCategories)
        .HasForeignKey(pc => pc.CategoryID);

}
if (User.Identity.IsAuthenticated)
{
    var userID = User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;

    var categories = _applicationDbContext.Users.Where(p => p.Id == userID)
                        .SelectMany(p => p.ApplicationUserCategories)
                        .Select(pc => pc.Category).ToList();

}