【问题标题】:Many to Many relationship in Asp.Net MVC 5 with Identity table and Custom tableAsp.Net MVC 5中的多对多关系与身份表和自定义表
【发布时间】:2017-02-02 05:27:07
【问题描述】:

我正在尝试将 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
}

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

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

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

【问题讨论】:

    标签: entity-framework asp.net-mvc-5 ef-code-first foreign-keys entity-framework-6


    【解决方案1】:

    试试这样:

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

    以及正确的架构:

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

    【讨论】:

    • 我希望我的手指远离 ApplicationDbContext
    • 您无法将两个不同的上下文与两个不同的表一起使用,然后在相同的 linq 查询中查询它们。您只能使用一种上下文。
    • 你能展示一下这些小变化吗?希望它会帮助其他人@Arianit
    • @Arianit 发生了什么变化?在我身边就是这样。
    • 它们应该以集合的方式编写,例如: public virtual ICollection ApplicationUser{get;set} ,但在降价中,符号“
    【解决方案2】:

    错误文本与您的多对多关系无关。它提示其他内置实体未正确配置。因此,如果您提供自定义 DbContext 类的完整定义及其配置方式,那就太好了。

    更新

    据我了解,您正在使用两种不同的环境。您必须使用相同的上下文,因为您正在扩展 IdentityContext、创建关系和添加自定义类型。所以问题会自己解决。 希望,这会有所帮助。

    【讨论】:

    • 但是使用 applicationdbcontext 处理业务逻辑是否可以接受?
    • 别忘了 - 它只是 DbContext。 ApplicationDbContext 本身充当存储库。每个业务逻辑都需要一个存储库来持久化或读取数据。简单的解决方案可以直接使用 ApplicationDbContext,在复杂的情况下 - 你创建自己的存储库。这也是一堆带有身份验证系统的东西。
    • 但困扰我的是,将来当我需要使用我的自定义数据库表时,例如从另一个表或另一个表引用它,我必须使用 ApplicationDbContext!这是我有点不喜欢的事情。我只想将 ApplicationDbContext 用于身份
    【解决方案3】:

    您可以使用Fluent API进行如下尝试。

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    
        modelBuilder.Entity<Task>()
                    .HasMany<ApplicationUser>(s => s.Users)
                    .WithMany(c => c.Tasks)
                    .Map(cs =>
                            {
                                cs.MapLeftKey("TaskRefId");
                                cs.MapRightKey("ApplicationUserRefId");
                                cs.ToTable("TaskApplicationUser");
                            });
    
    }
    

    更新:你也可以看到这个链接。

    EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType

    【讨论】:

    • 这必须在我自己的上下文类中,而不是在 ApplicationDbContext 类中,对吧?
    • 我没听明白?
    • 有 2 个上下文类,一个用于 Asp.Net 为我生成的 Identity,称为 ApplicationDbContext。还有我自己制作的另一个背景。因此,您作为答案编写的这段代码必须在我的 Context 类中。还是?
    • 是的,使用你的context
    • 请查看更新
    猜你喜欢
    • 2016-11-21
    • 1970-01-01
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-11
    相关资源
    最近更新 更多