Asp.Net MVC 5中的多对多关系与身份表和自定义表 [英] Many to Many relationship in Asp.Net MVC 5 with Identity table and Custom table

查看:25
本文介绍了Asp.Net MVC 5中的多对多关系与身份表和自定义表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Asp.Net Identity 生成的表中的用户与我自己的表建立关系.这种关系必须是多对多的,因为许多用户可以处理同一个任务(这是我的表),而一个用户可以同时处理多个任务.

I'm trying to make a relationship between the Users from the table generated by Asp.Net Identity with my own table. The relationship must be many to many, since many Users can work on the same Task (which is my table), and same time an User can work on multiple Tasks.

public class Task
{      
    public int ID { get; set; }
    public string Name { get; set; }

    public string UserID { get; set; }

    public virtual ICollection<ApplicationUser> Users { get; set; }
}

public class ApplicationUser : IdentityUser
{
    public int TaskID { get; set; }
    public virtual ICollection<Task> Tasks{ get; set; }

    // rest of the code
}

我尝试过这种方式,但在迁移(或运行时)期间出现错误

I try it this way but I get an error during migration (or run time)

在模型生成过程中检测到一个或多个验证错误:"

"One or more validation errors were detected during model generation:"

请帮我解决这个问题并存档我需要的东西.

Please help me solve this problem and archive what I need.

推荐答案

试试这样:

public class Projects
{
    public Projects()
    {
        ApplicationUser = new HashSet<ApplicationUser>();
    }
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<ApplicationUser> ApplicationUser { get; set;     }
}

  • 应用用户

  • Application User

    
    
        public class ApplicationUser : IdentityUser
        {
            public ApplicationUser()
            {
                Projects = new HashSet<Projects>();
            }
            public async Task GenerateUserIdentityAsync(UserManager manager)
            {
                // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
                var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
                // Add custom user claims here
                return userIdentity;
            }        
            public virtual ICollection <Projects > Projects { get; set; }
        }
    
    

    1. 应用上下文:

    
    
        public class ApplicationDbContext : IdentityDbContext
        {
            public ApplicationDbContext()
                : base("DefaultConnection", throwIfV1Schema: false)
            {
            }
            public virtual DbSet<Projects> Projects { get; set; }
    
            public static ApplicationDbContext Create()
            {
                return new ApplicationDbContext();
            }
        }
    
    

    现在当我运行这个 Mvc 应用程序并注册时,我得到的 db 表如下所示:

    now when I run this Mvc app and register, the db tables I get is like the following:

    以及正确的架构:

    要质疑的事情很多,在我看来,重要的是确定你是否:- 你可以/应该混合应用程序上下文和你的模型上下文吗?

    The things to be questioned are a lot, from my point of view important is to determine if you: - can/should you mix application context and your model context ?

    这篇关于Asp.Net MVC 5中的多对多关系与身份表和自定义表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆