gpt4 book ai didi

entity-framework - Asp.Net MVC 5 中的多对多关系与身份表和自定义表

转载 作者:行者123 更新时间:2023-12-03 23:38:43 25 4
gpt4 key购买 nike

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

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
}

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

“在模型生成过程中检测到一个或多个验证错误:” enter image description here

请帮我解决这个问题,把我需要的存档。

最佳答案

像这样尝试:

  1. 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; }
    }
  2. 应用程序用户



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 应用程序并注册时,我得到的数据库表如下所示:

From MSSQL

和正确的架构:

enter image description here

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

关于entity-framework - Asp.Net MVC 5 中的多对多关系与身份表和自定义表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39676239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com