gpt4 book ai didi

asp.net-core-2.1 - 如何在 asp.net Core 2.1.1 中为 IdentityUser 创建自定义字段

转载 作者:行者123 更新时间:2023-12-04 20:28:34 25 4
gpt4 key购买 nike

我创建了一个扩展“IdentityUser”类的模型。

public class ApplicationUser : IdentityUser
{
public string FirstName_TXT { get; set; }
public string LastName_TXT { get; set; }
}

在定义 dbcontext 时,我确保包含 IdentityDbContext 以及 ApplicationUser Reference。
//initial DB Configuration
public class DbContext : IdentityDbContext<ApplicationUser>
{
//Users References Articles
//Articles Automaticalled gets create
public DbSet<ContactRequest> ContactRequests { get; set; }
public DbSet<Class> Classes { get; set; }

public DbContext()
{

}

public DbContext(DbContextOptions options)
: base(options)
{

}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=.\SQLEXPRESS;Database=DBNAME;Trusted_Connection=True;");
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}

当我尝试在控制台应用程序中使用附加字段创建数据库时,出现以下错误。
class Program
{
static void Main(string[] args)
{

using (var ctx = new DbContext())
{
ctx.Database.EnsureCreatedAsync();

ctx.SaveChanges();

}
}
}

System.InvalidOperationException: 'A key cannot be configured on 'ApplicationUser' because it is a derived type. The key must be configured on the root type 'IdentityUser'. If you did not intend for 'IdentityUser' to be included in the model, ensure that it is not included in a DbSet property on your context, referenced in a configuration call to ModelBuilder, or referenced from a navigation property on a type that is included in the model.'



不知道我错过了什么。任何建议将不胜感激。

最佳答案

key 属性不能被设计忽略。 IdentityUser有一个默认的键属性 身份证 .
您的 ApplicationUser 类没有指定其 Id 字段的类型。我把我的代码

public class ApplicationUser : IdentityUser<string>
{
public string FirstName_TXT { get; set; }
public string LastName_TXT { get; set; }
}

您可以使用整数类型 Id 键属性,为此用途 IdentityUser<int>

关于asp.net-core-2.1 - 如何在 asp.net Core 2.1.1 中为 IdentityUser 创建自定义字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51699836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com