我陷入了一个奇怪的问题。我必须使用数据库优先方法启动ASP.NET MVC项目。我想使用内置标识进行身份验证和授权。
我已经在包含模型和其他类的类库项目中成功添加了存储库模式。
添加了对我的ASP.NET MVC项目的引用,并更改了项目的连接字符串以及上下文
这是两个项目的连接字符串
<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" />
以下是ApplicationUser和DoctorsDbContext类的代码
public class ApplicationUser : IdentityUser
{
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;
}
}
public class DoctorsDbContext : IdentityDbContext<ApplicationUser>
{
static DoctorsDbContext()
{
Database.SetInitializer<IdentityDbContext>(null);
Database.SetInitializer<DoctorsDbContext>(null);
}
public DoctorsDbContext()
: base("DoctorsDBEntities", throwIfV1Schema: false)
{
Database.SetInitializer<IdentityDbContext>(null);
Database.SetInitializer<DoctorsDbContext>(null);
}
public static DoctorsDbContext Create()
{
return new DoctorsDbContext();
}
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");
}
现在收到此错误
答案 0 :(得分:0)
如果我理解正确,您使用默认的Microsoft身份类。并且您希望通过db first context来控制它。 如果是,那么:
您应该继承IdentityDbContext<ApplicationUser>
而不是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");
}
和ApplicationUser:
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;
}
}
并用DoctorsDbContext替换身份生成的类和方法中的所有Dbcontext。
答案 1 :(得分:0)
我做错了。使用的连接字符串是Entity Framework,它不适用于Identity。
只需为身份添加另一个连接字符串即可使用
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=DoctorsDB;Integrated Security=True" providerName="System.Data.SqlClient" />