Asp.net mvc 5 将ApplicationDbContext和DataContext合并到单个上下文中
我在合并这两个上下文时遇到问题 我从IdentityModels.cs中删除了这行代码:Asp.net mvc 5 将ApplicationDbContext和DataContext合并到单个上下文中,asp.net-mvc-5,Asp.net Mvc 5,我在合并这两个上下文时遇到问题 我从IdentityModels.cs中删除了这行代码: public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("DefaultConnection") { } } public类ApplicationDbContext:IdentityDbCo
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
}
public类ApplicationDbContext:IdentityDbContext
{
公共应用程序上下文()
:base(“默认连接”)
{
}
}
我在DataContext.cs中实现了这一点
public class DataContext : DbContext
{
public DataContext()
: base("name=DefaultConnection")
{
}
// Account tables
public DbSet ApplicationUsers { get; set; }
public DbSet Roles { get; set; }
public DbSet Tokens { get; set; }
public DbSet UserClaims { get; set; }
public DbSet UserLogins { get; set; }
public DbSet UserManagements { get; set; }
public DbSet UserRoles { get; set; }
public DbSet UserSecrets { get; set; }
// App Models
public DbSet<Course> Courses { get; set; }
public DbSet<Student> Students { get; set; }
}
公共类DataContext:DbContext
{
公共数据上下文()
:base(“name=DefaultConnection”)
{
}
//科目表
公共数据库集应用程序用户{get;set;}
公共数据库集角色{get;set;}
公共DbSet令牌{get;set;}
公共DbSet UserClaims{get;set;}
公共数据库集用户登录名{get;set;}
公共数据库集用户管理{get;set;}
公共数据库集用户角色{get;set;}
公共数据库集用户机密{get;set;}
//应用程序模型
公共数据库集课程{get;set;}
公共数据库集学生{get;set;}
}
问题:
当我“更新数据库”时,它只创建了课程和学生表
问题
如果成功实现此方法,我将丢失IdentityDbContext接口提供的所有优秀方法,例如:
var rm = new RoleManager<IdentityRole>(
new RoleStore<IdentityRole>(new ApplicationDbContext()));
return rm.RoleExists(name);
var rm=新角色管理器(
新角色存储库(新的ApplicationDbContext());
返回rm.RoleExists(名称);
您必须使用:
DbSet Name{get;set;}
而不是
DbSet Model {get;set;}
作为你问题的答案。角色存储需要DbContext(IdentityDbContext从DbContext继承)。因此,您应该仍然能够使用roleManager和其他功能……您必须使用:
DbSet Name{get;set;}
而不是
DbSet Model {get;set;}
作为你问题的答案。角色存储需要DbContext(IdentityDbContext从DbContext继承)。因此,您应该仍然能够使用roleManager和更多…好的,这是我在本主题中找到的解决方案,由Olav Nybo发布 转到他在github上的示例项目: 从“模型”文件夹下载“配置”文件夹,并将该文件夹放在“模型”文件夹中 在DataContext文件中,您将放置这段代码,它将调用这些配置文件来构建数据库
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
if (modelBuilder == null)
throw new ArgumentNullException("modelBuilder");
modelBuilder.Configurations.AddFromAssembly(typeof(Generic_Repo_Template.Models.Configurations.ApplicationUserConfiguration).Assembly);
}
现在数据库已经创建,您仍然无法通过datacontext对象访问这些表。为了能够通过datacontext访问这些表,需要包括以下代码行:
public virtual IDbSet<ApplicationUser> Users { get; set; }
public virtual IDbSet<IdentityRole> Roles { get; set; }
public virtual IDbSet<IdentityUserClaim> Claims { get; set; }
公共虚拟IDbSet用户{get;set;}
公共虚拟IDbSet角色{get;set;}
公共虚拟IDbSet声明{get;set;}
因此,完整的DataContext文件将如下所示:
public class DataContext : DbContext
{
public DataContext()
: base("name=DefaultConnection")
{
}
// Account tables
public virtual IDbSet<ApplicationUser> Users { get; set; }
public virtual IDbSet<IdentityRole> Roles { get; set; }
public virtual IDbSet<IdentityUserClaim> Claims { get; set; }
// App Models
public DbSet<Course> Courses { get; set; }
public DbSet<Student> Students { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
if (modelBuilder == null)
throw new ArgumentNullException("modelBuilder");
modelBuilder.Configurations.AddFromAssembly(typeof(AspNetRoleBasedSecurity.Models.Configurations.ApplicationUserConfiguration).Assembly);
}
}
公共类DataContext:DbContext
{
公共数据上下文()
:base(“name=DefaultConnection”)
{
}
//科目表
公共虚拟IDbSet用户{get;set;}
公共虚拟IDbSet角色{get;set;}
公共虚拟IDbSet声明{get;set;}
//应用程序模型
公共数据库集课程{get;set;}
公共数据库集学生{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
if(modelBuilder==null)
抛出新的ArgumentNullException(“modelBuilder”);
modelBuilder.Configurations.AddFromAssembly(typeof(AspNetRoleBasedSecurity.Models.Configurations.ApplicationUserConfiguration.Assembly);
}
}
好的,这是我在Olav Nybo发布的本主题中找到的解决方案
转到他在github上的示例项目:
从“模型”文件夹下载“配置”文件夹,并将该文件夹放在“模型”文件夹中
在DataContext文件中,您将放置这段代码,它将调用这些配置文件来构建数据库
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
if (modelBuilder == null)
throw new ArgumentNullException("modelBuilder");
modelBuilder.Configurations.AddFromAssembly(typeof(Generic_Repo_Template.Models.Configurations.ApplicationUserConfiguration).Assembly);
}
现在数据库已经创建,您仍然无法通过datacontext对象访问这些表。为了能够通过datacontext访问这些表,需要包括以下代码行:
public virtual IDbSet<ApplicationUser> Users { get; set; }
public virtual IDbSet<IdentityRole> Roles { get; set; }
public virtual IDbSet<IdentityUserClaim> Claims { get; set; }
公共虚拟IDbSet用户{get;set;}
公共虚拟IDbSet角色{get;set;}
公共虚拟IDbSet声明{get;set;}
因此,完整的DataContext文件将如下所示:
public class DataContext : DbContext
{
public DataContext()
: base("name=DefaultConnection")
{
}
// Account tables
public virtual IDbSet<ApplicationUser> Users { get; set; }
public virtual IDbSet<IdentityRole> Roles { get; set; }
public virtual IDbSet<IdentityUserClaim> Claims { get; set; }
// App Models
public DbSet<Course> Courses { get; set; }
public DbSet<Student> Students { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
if (modelBuilder == null)
throw new ArgumentNullException("modelBuilder");
modelBuilder.Configurations.AddFromAssembly(typeof(AspNetRoleBasedSecurity.Models.Configurations.ApplicationUserConfiguration).Assembly);
}
}
公共类DataContext:DbContext
{
公共数据上下文()
:base(“name=DefaultConnection”)
{
}
//科目表
公共虚拟IDbSet用户{get;set;}
公共虚拟IDbSet角色{get;set;}
公共虚拟IDbSet声明{get;set;}
//应用程序模型
公共数据库集课程{get;set;}
公共数据库集学生{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
if(modelBuilder==null)
抛出新的ArgumentNullException(“modelBuilder”);
modelBuilder.Configurations.AddFromAssembly(typeof(AspNetRoleBasedSecurity.Models.Configurations.ApplicationUserConfiguration.Assembly);
}
}
要将数据库从ApplicationDbContext更新到DataContext,请打开软件包管理器并键入:启用迁移-强制(enter)添加迁移(enter)更新数据库(enter)如果使用“添加迁移”时出错,请删除迁移文件夹中的所有文件,然后重试。若要将数据库从ApplicationDbContext更新到DataContext,请打开包管理器并键入:启用迁移-强制(enter)添加迁移(enter)更新数据库(enter)如果使用“添加迁移”时出错,请删除迁移文件夹中的所有文件,然后重试。