我正在构建一个单页应用程序,因此我使用了Visual Studio默认模板。在开发过程中,我有2个数据库Entityframework.mdf和Identity.mdf,因为这是默认配置,但是现在我需要与用户建立关系,并且由于他们位于另一个数据库中而无法轻松访问它们。
在MVC模板中,您可以像这样轻松地做到这一点:
public class ApplicationUser: IdentityUser
{
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public DbSet<CustomTableItem> CustomTable{ get; set; }
//You can add more tables
}
当您使用单页应用程序时,它以我不了解的方式配置用户管理。
UserManagerFactory = () => new UserManager<IdentityUser>(new UserStore<IdentityUser>());
然后从这篇文章
这段代码使用UserStore类的默认构造函数,该类将创建IdentityDbContext对象的新实例,因为未提供对象。如果您想使用自己的IdentityDbContext派生类(如MVC 5项目所做的那样),则可以修改上面的初始化代码并传递您自己的上下文。
它说我可以修改它,但是它没有显示:(,并且我已经尝试过了,但是我无法使其工作。这就是我正在尝试做的
UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());
我是小姐吗?
如果对UserStore
类使用默认构造函数(不带参数),则会发生以下情况:
public UserStore()
{
this..ctor((DbContext) new IdentityDbContext());
this.DisposeContext = true;
}
身份框架使用默认的连接字符串为您创建自己的数据库上下文,并且与您自己的模型或无关DbContext
。
斯科特在他的文章中说的是,UserStore
有一个定义如下的构造函数:
public UserStore(DbContext context)
{
base..ctor(context);
}
换句话说,您可以DbContext
将参数作为参数提供给的构造函数UserStore
:
UserManagerFactory = () => new UserManager<ApplicationUser>(
new UserStore<ApplicationUser>(new ApplicationDbContext()))
ApplicationDbContext
如您在问题中描述的那样定义哪里。
您将需要创建一个迁移,ApplicationDbContext
该迁移将在Entityframework.mdf中创建Identity表。然后,您必须将数据从Identity.mdf移到您的主数据库中。连接到两个数据库,并运行以下命令:
insert into EntityFramework.dbo.IdenetityUsers
select * from Identity.dbo.IdentityUsers
但是,我只完成了在单个SQL Server实例中从一个数据库到另一个数据库的数据迁移,而不是在LocalDb之间迁移(我假设您使用了这些数据库)。因此,此方法可能不起作用,您必须将数据从中导出Identity.mdf
到csv文件中,然后将其导入到EntityFramework.mdf
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句