结合 AppDbContext 和 IdentityDbContext 问题
Combining AppDbContext And IdentityDbContext Issue
我正在使用 EF 6.0 并希望将 AppDbContext
和 IdentityDbContext
合并到一个上下文中,即 AppDbContext
.
这是一项要求,因为我有其他 table 与 EF 创建的 AspNetUsers table 有关系。
问题是 EF 正在为用户创建两个 table,例如 AspNetUsers
和 IdentityUsers
。
此外,如果我在 DbContext 中使用 DbSet<ApplicationUser>
而不是 DbSet<IdentityUsers>
,则添加迁移会引发错误。
我的 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 Class 是
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 的东西。
原来我的服务层项目也需要引用 Microsoft.AspNet.Identity.EntityFramework.dll
。
一旦我添加了所有身份实体集现在都可用于我的上下文。
此外,tmg & Brendan 指出 IdentityDbContext
成员 DbSets
在AppDbContext
.
希望这对某人有所帮助。
我正在使用 EF 6.0 并希望将 AppDbContext
和 IdentityDbContext
合并到一个上下文中,即 AppDbContext
.
这是一项要求,因为我有其他 table 与 EF 创建的 AspNetUsers table 有关系。
问题是 EF 正在为用户创建两个 table,例如 AspNetUsers
和 IdentityUsers
。
此外,如果我在 DbContext 中使用 DbSet<ApplicationUser>
而不是 DbSet<IdentityUsers>
,则添加迁移会引发错误。
我的 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 Class 是
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 的东西。
原来我的服务层项目也需要引用 Microsoft.AspNet.Identity.EntityFramework.dll
。
一旦我添加了所有身份实体集现在都可用于我的上下文。
此外,tmg & Brendan 指出 IdentityDbContext
成员 DbSets
在AppDbContext
.
希望这对某人有所帮助。