2016-03-15 22 views
0

我正在使用EF 6.0並希望將AppDbContext & IdentityDbContext合併到單個上下文中,該上下文爲AppDbContextCombining AppDbContext和IdentityDbContext問題

這是一個需求,因爲我有其他表與EF創建的AspNetUsers表有關係。

問題是EF正在爲用戶創建兩個表格,如AspNetUsersIdentityUsers

另外如果我在DbContext中使用DbSet<ApplicationUser>而不是DbSet<IdentityUsers>,那麼add-migration會拋出錯誤。

我AppDbContext是

public class AppDbContext : IdentityDbContext<ApplicationUser>,IDisposable 
    { 
     public AppDbContext() 
      : base("MvcArchBSEFDB", throwIfV1Schema: false) 
     { 
     } 

     public static AppDbContext Create() 
     { 
      return new AppDbContext(); 
     } 

     protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder) 
     { 
      base.OnModelCreating(modelBuilder); 

     } 

     //public DbSet<ApplicationUser> AppUsers { get; set; } // breaks migration 
//Multiple object sets per type are not supported. The object sets 'AppUsers ' and 'Users' can both contain instances of type 'MvcArchBS.DAL.Setp.ApplicationUser'. 

     public DbSet<IdentityUser> AppUsers { get; set; } // Creates Two Tables AspNetUsers & IdentityUser 

     public DbSet<IdentityUserRole> UserRoles { get; set; } 
     public DbSet<IdentityUserClaim> Claims { get; set; } 
     public DbSet<IdentityUserLogin> Logins { get; set; } 

     public DbSet<Module> Modules { get; set; } 
     public DbSet<SubModule> SubModules { get; set; } 
     public DbSet<PageMst> Pages { get; set; } 



     public void Dispose() 
     { 
      base.Dispose(); 
     } 

    } 

而且我ApplicationUser類是

public class ApplicationUser : IdentityUser 
{ 
    public ApplicationUser() 
    { 
     status = "A"; 
     reptngusrid = "admin"; 
    } 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int usrid { get; set; } 
    public string usrdescr { get; set; } 
    public int empid { get; set; } 
    public int usrgrpid { get; set; } 
    [StringLength(1)] 
    public string status { get; set; } 

    public string reptngusrid { get; set; } 
    public int defmodid { get; set; } 
    [StringLength(20)] 
    public string cltur { get; set; } 


    [ForeignKey("defmodid")] 
    public virtual Module Module { get; set; } 

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> 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; 
    } 
} 

我如何得到這個工作?我想在我的查詢中使用context.AppUsers之類的東西,這是我無法得到的。

+1

IdentityDbContext 已經有DbSet Users {get;組; }。所有其他身份實體也一樣。你不應該包含它們。 – tmg

+0

@tmg是的。但它不顯示在AppDbContext屬性 – Deb

+1

@Deb它應該在那裏作爲用戶,而不是AppUsers。根據上面的@tmg,你也應該刪除'UserRoles','Claims'和'Logins' - 它們都已經在'IdentityDbContext'中定義了。 –

回答

0

原來我的服務層項目也需要參考Microsoft.AspNet.Identity.EntityFramework.dll

一旦我添加了所有身份實體集現在可用我的上下文。

另外,作爲TMG & 布倫丹指出了IdentityDbContext成員DbSets沒有在AppDbContext需要。

希望這可以幫助別人。