Asp.net mvc Asp标识自定义上下文
我正在构建一个单页应用程序,因此使用了VisualStudio默认模板。 在开发时,我有两个数据库Entityframework.mdf和Identity.mdf,因为默认配置就是这样做的,但现在我需要与用户建立关系,我无法轻松联系到他们,因为他们在另一个数据库中 在MVC模板中,您可以轻松地执行以下操作:Asp.net mvc Asp标识自定义上下文,asp.net-mvc,entity-framework,asp.net-web-api,single-page-application,asp.net-identity,Asp.net Mvc,Entity Framework,Asp.net Web Api,Single Page Application,Asp.net Identity,我正在构建一个单页应用程序,因此使用了VisualStudio默认模板。 在开发时,我有两个数据库Entityframework.mdf和Identity.mdf,因为默认配置就是这样做的,但现在我需要与用户建立关系,我无法轻松联系到他们,因为他们在另一个数据库中 在MVC模板中,您可以轻松地执行以下操作: public class ApplicationUser: IdentityUser { } public class ApplicationDbContext : IdentityDbCo
public class ApplicationUser: IdentityUser
{
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public DbSet<CustomTableItem> CustomTable{ get; set; }
//You can add more tables
}
public UserStore(DbContext context)
{
base..ctor(context);
}
insert into EntityFramework.dbo.IdenetityUsers
select * from Identity.dbo.IdentityUsers
公共类应用程序用户:IdentityUser
{
}
公共类ApplicationDbContext:IdentityDbContext
此代码使用UserStore类的默认构造函数,该类将创建IdentityDbContext对象的新实例,因为未提供对象。如果您想像MVC5项目一样使用自己的IdentityDbContext派生类,可以修改上述初始化代码并在自己的上下文中传递
它说我可以修改它,但它没有显示如何:(,我已经尝试了,但我无法使它工作。这就是我正在尝试做的
UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());
UserManagerFactory=()=>newusermanager(newuserstore());
我缺少什么?如果对UserStore
类使用默认构造函数(不带参数),则会发生以下情况:
public UserStore()
{
this..ctor((DbContext) new IdentityDbContext());
this.DisposeContext = true;
}
Identity framework使用默认连接字符串为您创建自己的数据库上下文,并且与您自己的模型或DbContext
没有关系
Scott在他的文章中说,UserStore
有一个如下定义的构造函数:
public class ApplicationUser: IdentityUser
{
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public DbSet<CustomTableItem> CustomTable{ get; set; }
//You can add more tables
}
public UserStore(DbContext context)
{
base..ctor(context);
}
insert into EntityFramework.dbo.IdenetityUsers
select * from Identity.dbo.IdentityUsers
换句话说,您可以将DbContext
作为参数提供给UserStore
的构造函数:
UserManagerFactory = () => new UserManager<ApplicationUser>(
new UserStore<ApplicationUser>(new ApplicationDbContext()))
但是,我只在单个SQL Server实例中完成了从一个数据库到另一个数据库的数据迁移,而没有在LocalDB之间进行迁移(我假设您使用了这些数据库)。因此,此方法可能不起作用,您必须将数据从Identity.mdf
导出到csv文件中,然后将其导入EntityFramework.mdf