C# 无法将内置标识类与数据库优先开发一起使用
我陷入了一个奇怪的问题。我必须用数据库优先的方法开始一个ASP.NETMVC项目。我想使用内置标识进行身份验证和授权 我已经在一个带有模型和其他类的类库项目中成功地添加了存储库模式 添加了对我的ASP.NET MVC项目的引用,并随上下文一起更改了项目的连接字符串 这是两个项目的连接字符串C# 无法将内置标识类与数据库优先开发一起使用,c#,asp.net-mvc,asp.net-identity,class-library,C#,Asp.net Mvc,Asp.net Identity,Class Library,我陷入了一个奇怪的问题。我必须用数据库优先的方法开始一个ASP.NETMVC项目。我想使用内置标识进行身份验证和授权 我已经在一个带有模型和其他类的类库项目中成功地添加了存储库模式 添加了对我的ASP.NET MVC项目的引用,并随上下文一起更改了项目的连接字符串 这是两个项目的连接字符串 <add name="DoctorsDBEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl
<add name="DoctorsDBEntities"
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;
provider=System.Data.SqlClient;
provider connection string="data source=(LocalDb)\MSSQLLocalDB;initial catalog=DoctorsDB;
integrated security=True;
MultipleActiveResultSets=True;
App=EntityFramework""
providerName="System.Data.EntityClient" />
如果我理解正确,您将使用默认的Microsoft标识类。您希望通过db-first上下文来控制它。
如果可以,则:
您应该继承自IdentityDbContext
而不是Dbcontext
public class DoctorsDbContext : IdentityDbContext<ApplicationUser>
{
static DoctorsContext()
{
Database.SetInitializer<IdentityDbContext>(null);
Database.SetInitializer<DoctorsDbContext>(null);
}
public DoctorsDbContext()
: base("Name=nameOfConnectionString")
{
Database.SetInitializer<IdentityDbContext>(null);
Database.SetInitializer<DoctorsDbContext >(null);
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>()
.ToTable("dbo.AspNetUsers");
modelBuilder.Entity<ApplicationUser>()
.ToTable("dbo.AspNetUsers");
modelBuilder.Entity<IdentityRole>()
.ToTable("dbo.AspNetRoles");
modelBuilder.Entity<ApplicationRole>()
.ToTable("dbo.AspNetRoles");
modelBuilder.Entity<IdentityUserClaim>().ToTable("dbo.AspNetUserClaims");
modelBuilder.Entity<IdentityUserRole>().ToTable("dbo.AspNetUserRoles");
modelBuilder.Entity<IdentityUserRole>().HasKey((IdentityUserRole r) => new { UserId = r.UserId, RoleId = r.RoleId }).ToTable("dbo.AspNetUserRoles");
modelBuilder.Entity<IdentityUserLogin>().ToTable("dbo.AspNetUserLogins");
}
公共类DoctorsDbContext:IdentityDbContext
{
静态DoctorsContext()
{
Database.SetInitializer(null);
Database.SetInitializer(null);
}
公共DoctorsDbContext()
:base(“Name=nameOfConnectionString”)
{
Database.SetInitializer(null);
Database.SetInitializer(null);
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove();
基于模型创建(modelBuilder);
modelBuilder.Entity()
.ToTable(“dbo.AspNetUsers”);
modelBuilder.Entity()
.ToTable(“dbo.AspNetUsers”);
modelBuilder.Entity()
.ToTable(“dbo.AspNetRoles”);
modelBuilder.Entity()
.ToTable(“dbo.AspNetRoles”);
modelBuilder.Entity().ToTable(“dbo.AspNetUserClaims”);
modelBuilder.Entity().ToTable(“dbo.AspNetUserRoles”);
modelBuilder.Entity().HasKey((IdentityUserRole r)=>new{UserId=r.UserId,RoleId=r.RoleId}).ToTable(“dbo.AspNetUserRoles”);
modelBuilder.Entity().ToTable(“dbo.AspNetUserLogins”);
}
和应用程序用户:
public class ApplicationUser : IdentityUser
{
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;
}
}
公共类应用程序用户:IdentityUser
{
GenerateUserIdentityAsync(用户管理器)
{
//注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
//在此处添加自定义用户声明
返回用户身份;
}
}
并将标识生成的类和方法中的所有Dbcontext替换为DoctorDBContext。如果我理解正确,您将使用默认的Microsoft标识类。并且您希望通过db-first上下文控制它。 如果可以,则: 您应该继承自
IdentityDbContext
而不是Dbcontext
public class DoctorsDbContext : IdentityDbContext<ApplicationUser>
{
static DoctorsContext()
{
Database.SetInitializer<IdentityDbContext>(null);
Database.SetInitializer<DoctorsDbContext>(null);
}
public DoctorsDbContext()
: base("Name=nameOfConnectionString")
{
Database.SetInitializer<IdentityDbContext>(null);
Database.SetInitializer<DoctorsDbContext >(null);
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>()
.ToTable("dbo.AspNetUsers");
modelBuilder.Entity<ApplicationUser>()
.ToTable("dbo.AspNetUsers");
modelBuilder.Entity<IdentityRole>()
.ToTable("dbo.AspNetRoles");
modelBuilder.Entity<ApplicationRole>()
.ToTable("dbo.AspNetRoles");
modelBuilder.Entity<IdentityUserClaim>().ToTable("dbo.AspNetUserClaims");
modelBuilder.Entity<IdentityUserRole>().ToTable("dbo.AspNetUserRoles");
modelBuilder.Entity<IdentityUserRole>().HasKey((IdentityUserRole r) => new { UserId = r.UserId, RoleId = r.RoleId }).ToTable("dbo.AspNetUserRoles");
modelBuilder.Entity<IdentityUserLogin>().ToTable("dbo.AspNetUserLogins");
}
公共类DoctorsDbContext:IdentityDbContext
{
静态DoctorsContext()
{
Database.SetInitializer(null);
Database.SetInitializer(null);
}
公共DoctorsDbContext()
:base(“Name=nameOfConnectionString”)
{
Database.SetInitializer(null);
Database.SetInitializer(null);
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove();
基于模型创建(modelBuilder);
modelBuilder.Entity()
.ToTable(“dbo.AspNetUsers”);
modelBuilder.Entity()
.ToTable(“dbo.AspNetUsers”);
modelBuilder.Entity()
.ToTable(“dbo.AspNetRoles”);
modelBuilder.Entity()
.ToTable(“dbo.AspNetRoles”);
modelBuilder.Entity().ToTable(“dbo.AspNetUserClaims”);
modelBuilder.Entity().ToTable(“dbo.AspNetUserRoles”);
modelBuilder.Entity().HasKey((IdentityUserRole r)=>new{UserId=r.UserId,RoleId=r.RoleId}).ToTable(“dbo.AspNetUserRoles”);
modelBuilder.Entity().ToTable(“dbo.AspNetUserLogins”);
}
和应用程序用户:
public class ApplicationUser : IdentityUser
{
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;
}
}
公共类应用程序用户:IdentityUser
{
GenerateUserIdentityAsync(用户管理器)
{
//注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
//在此处添加自定义用户声明
返回用户身份;
}
}
并用DoctorsDbContext替换标识生成的类和方法中的所有Dbcontext。我做错了。使用的连接字符串是实体框架的,不应用于标识 只需为identity添加另一个连接字符串,就可以了
我做错了。使用的连接字符串是实体框架的,不应该用于标识
只需为identity添加另一个连接字符串,就可以了
我按照你的建议编辑了代码,但是运气不好(参见上文,也编辑了帖子)检查:你应该有一个dbContext或IdentityDbContext(DoctorsDbContext)。编辑IdentityConfig.csI按照你的建议编辑了代码,但是运气不好(参见上文,也编辑了帖子)选中此项:您应该有一个dbContext或IdentityDbContext(DoctorSbContext)。然后编辑IdentityConfig.cs