加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-core-2.1 – 如何在asp.net Core 2.1.1中为IdentityUser

发布时间:2020-12-15 19:53:11 所属栏目:asp.Net 来源:网络整理
导读:我创建了一个扩展“IdentityUser”类的Model. public class ApplicationUser : IdentityUser{ public string FirstName_TXT { get; set; } public string LastName_TXT { get; set; }} 在定义dbcontext时,我确保包含IdentityDbContext以及ApplicationUser Re
我创建了一个扩展“IdentityUser”类的Model.
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.’

不确定我错过了什么.任何建议将不胜感激.

解决方法

关键属性不能被设计忽略. IdentityUser具有默认密钥属性Id.
您的ApplicationUser类未指定其Id字段的类型.我把我的代码
public class ApplicationUser : IdentityUser<string>
{
    public string FirstName_TXT { get; set; }
    public string LastName_TXT { get; set; }
}

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读