首页 文章

结合AppDbContext和IdentityDbContext问题

提问于
浏览
0

我正在使用EF 6.0并希望将 AppDbContextIdentityDbContext 组合到一个上下文中,即 AppDbContext .

这是一个要求,因为我有其他表与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 回答

  • 0

    事实证明我的服务层项目还需要对 Microsoft.AspNet.Identity.EntityFramework.dll 的引用 .

    一旦我添加了所有身份实体集现在可用我的上下文 .

    另据 tmgBrendan 指出 AppDbContext 成员_29678450_不需要 AppDbContext .

    希望这有助于某人 .

相关问题