Add-Migration 控制台命令无法创建类型为“”的“DbContext”

问题描述 投票:0回答:1

无法创建类型为“”的“DbContext”。异常“实体类型‘IdentityUser’需要定义主键”。如果您打算使用无键实体类型,请在“OnModelCreating”中调用“HasNoKey”。有关无密钥实体类型的更多信息,请参阅 https://go.microsoft.com/fwlink/?linkid=2141943。' 在尝试创建实例时抛出。有关设计时支持的不同模式,请参阅 https://go.microsoft.com/fwlink/?linkid=851728

这是我的 IdentityUser 类:

using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;

namespace AgiznaProject.Models
{
    public class IdentityUser
    {
        
        public int AccountId { get; set; }
        public Guid AccountGuid { get; set; }
    
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Address { get; set; }
        public string ContactNumber { 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; }
    }
}

我尝试为我的数据库设计创建一个迁移。它不会让我帮忙,谢谢大家。

c# asp.net-mvc entity-framework-migrations
1个回答
0
投票

您需要向类添加一个属性来说明您想要的键是什么。

我假设您想要使用以下其中一项作为键,因此将 key 属性添加到其中之一。

public class IdentityUser
{
    [Key]
    public int AccountId { get; set; }

    // OR

    [Key]
    public Guid AccountGuid { get; set; }
}
© www.soinside.com 2019 - 2024. All rights reserved.