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