3

我试图使Asp.Net身份与我自己的表生成的表中的用户之间的关系。这种关系必须是多对多的,因为许多用户可以在同一个任务(这是我的表)上工作,同时用户可以在多个任务上工作。Asp.Net MVC 5与身份表和自定义表中的多对多关系

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

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

回答

4

尝试这样的:

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

  • 应用上下文:
  • 
    
        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

    被质疑的东西很多,从我的角度来看重要的是要确定我你: - 可以/你应该混合应用程序上下文和你的模型上下文吗?

    +0

    我宁愿保持我的手指远离ApplicationDbContext – Arianit

    +1

    没有办法,你可以使用两个不同的上下文与两个不同的表,然后在同一个linq查询中查询它们。你被限制使用一个上下文。 –

    +0

    我让它在一些小的变化下工作 – Arianit

    1

    您可以使用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

    +0

    这个必须在我自己的上下文类中不在ApplicationDbContext类中,对吗? – Arianit

    +0

    我没有找到你? – Sampath

    +0

    有两个上下文类,一个是Asp.Net为我生成的Identity,名为ApplicationDbContext。而我自己创造的另一个背景。所以你写的答案必须在我的Context类中,我猜。要么? – Arianit

    1

    错误文本与您的多对多关系无关。它提示其他内置实体配置不正确。所以,如果您提供了自定义DbContext类的完整定义以及它如何配置,那将会很好。

    UPDATE

    我的理解u的有两个不同的上下文中工作。您必须使用相同的上下文,因为您正在扩展IdentityContext,创建关系并添加自定义类型。所以问题会自行解决。 希望,这会有所帮助。

    +0

    ,但是使用applicationdbcontext来处理业务是可以接受的做法逻辑? – Arianit

    +1

    别忘了 - 它只是** DbContext **。 ApplicationDbContext本身就是存储库。每个业务逻辑都需要一个存储库来保存或读取数据。简单的解决方案可以直接使用ApplicationDbContext,复杂的 - 你可以创建你自己的仓库。这也是一个认证系统。 – Ryan

    +0

    但困扰我的是,将来当我需要使用自定义数据库表,例如从其他表或另一个表引用它时,我必须使用ApplicationDbContext!这是我有点不喜欢的事情。我想使用ApplicationDbContext仅用于Identity – Arianit