C# 实体类型';IdentityUserLogin<;字符串>';添加索引时需要定义主键
我在尝试向列添加索引时遇到此错误。你知道我为什么和如何解决这个问题吗 这就是我在AppDbContext文件中添加索引的方式C# 实体类型';IdentityUserLogin<;字符串>';添加索引时需要定义主键,c#,asp.net-core,entity-framework-core,ef-code-first,C#,Asp.net Core,Entity Framework Core,Ef Code First,我在尝试向列添加索引时遇到此错误。你知道我为什么和如何解决这个问题吗 这就是我在AppDbContext文件中添加索引的方式 protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Category>() .HasIndex(b => b.CategoryName).Is
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Category>()
.HasIndex(b => b.CategoryName).IsUnique();
}
public DbSet<Category> Categories { get; set; }
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
modelBuilder.Entity()
.HasIndex(b=>b.CategoryName).IsUnique();
}
公共数据库集类别{get;set;}
这就是我试图添加到索引中的模型
public class Category
{
public int CategoryId { get; set; }
[Required]
public string CategoryName { get; set; }
public string Description { get; set; }
public List<unitItem> itemList { get; set; }
}
公共类类别
{
public int CategoryId{get;set;}
[必需]
公共字符串CategoryName{get;set;}
公共字符串说明{get;set;}
公共列表项列表{get;set;}
}
我知道这个错误是在这里抛出的
AppDbContext context = applicationBuilder.GetRequiredService<AppDbContext>();
if (!context.Categories.Any())
{
context.Categories.AddRange(Categories.Select(c => c.Value));
}
AppDbContext context=applicationBuilder.GetRequiredService();
如果(!context.Categories.Any())
{
context.Categories.AddRange(Categories.Select(c=>c.Value));
}
当我尝试初始化我的DB时,调用你的
OnModelCreating
方法中的基类IdentityDbContext
使用OnModelCreating
以及配置其类(如IdentityUserLogin
):
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
//首先调用基类:
基于模型创建(modelBuilder);
modelBuilder.Entity()
.HasIndex(b=>b.CategoryName).IsUnique();
}
Thank打算再花一次时间寻找此解决方案。非常感谢。