我在 linux 上使用 dotnet core 1.1,当我想从常规 dbContext 中分离 Identity Context 时,我遇到了一些问题,无论何时我在 startup.cs 中运行以下代码行—— > configure:
//... some other services
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
serviceScope.ServiceProvider.GetService<ApplicationDbContext>().Database.Migrate();
//running other database.migrations here + seeding data. But it is the line above that causes problems
因此,这一行引发了异常: 实体类型“ IdentityUserLogin”需要定义一个主键
我就是不明白,为什么我的工作就是给 IdentityUserLogin 一个主键?这是第三方课程,我还没碰过呢。我有以下简单的设置:
namespace EsportshubApi.Models
{
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
public ApplicationDbContext()
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
}
以及应用程序用户:
namespace EsportshubApi.Models.Entities
{
public class ApplicationUser : IdentityUser
{
public ApplicationUser() { }
public static ApplicationUserBuilder Builder()
{
return new ApplicationUserBuilder(new ApplicationUser());
}
public int AccountId { get; set; }
public Guid AccountGuid { get; set; }
public string Salt { get; set; }
public bool Verified { get; set; }
public string Checksum { get; set; }
public string Password { get; set; }
public DateTime Created { get; set; }
public DateTime Updated { get; set; }
}
}
在我的创业过程中,我通过以下方式配置身份框架:
ConfigureServices:
services.AddEntityFrameworkSqlServer().AddMySQL().AddDbContext<ApplicationDbContext>(options =>
options.UseMySQL(config["ConnectionStrings:DefaultConnection"]));
还有
配置:
app.UseIdentity();
我的项目是开源的: 我的 Github 回购
如果有帮助的话。
我试过很多方法。最有前途的两个方法是派生这个通用显示中使用的所有类,并显式地传递它们,试图将它们的所有键更改为 int 等等。这就给出了 int而不是字符串的完全相同的错误。我尝试的另一种方法是在 OnModelCreating内部执行以下操作,例如:
modelBuilder.Entity<IdentityUserLogin<int>>()
.Property(login => login.UserId)
.ForMySQLHasColumnType("PK")
.UseSqlServerIdentityColumn()
.UseMySQLAutoIncrementColumn("AI");
正如你所看到的,这是回到了我把 UserId 作为一个整数的时候,但是我甚至不确定 UserId 是否应该是它的主键。我可以得到其他的错误,而不是这个
IdentityUserLogin 是层次结构的一部分,它没有识别器值
但是如果我有鉴别器的值,它最终会回到这个错误。我认为最奇怪的部分是,我有完全相同的实现作为 UnicornStore github 的例子,使用了一点身份框架,以及..。所以我真的需要你们的帮助。可以通过下载项目,将 default.appsettings.json复制到 appsettings.json,放入一个有效的连接字符串 dotnet restore,与 dotnet run --environment Development一起运行来重现这个错误。
我甚至尝试将实现改为使用 MSSQL 数据库而不是 MySQL,但是这也产生了同样的错误。