我正在使用EF 6.0并希望合并AppDbContext
& IdentityDbContext
放入AppDbContext
的单个上下文。
这是一项要求,因为我有其他表与EF创建的AspNetUsers表有关系。
问题是EF正在为用户创建两个表,例如AspNetUsers
和IdentityUsers
。
此外,如果我在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的东西,我无法得到。
答案 0 :(得分:0)
事实证明,我的服务层项目还需要引用Microsoft.AspNet.Identity.EntityFramework.dll
。
我添加了所有身份实体集现在可用我的上下文。
同样 tmg &amp; Brendan 指出IdentityDbContext
中不需要DbSets
成员AppDbContext
。
希望这有助于某人。