Add-Migration console command unable to create 'DbContext' of type ''

30 Views Asked by At

Unable to create a 'DbContext' of type ''. The exception 'The entity type 'IdentityUser' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. For more information on keyless entity types, see https://go.microsoft.com/fwlink/?linkid=2141943.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

Here is my IdentityUser Class:

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; }
    }
}

I tried to create a migration-for my datatabase design. It won't let me please help, Thanks guys.

1

There are 1 best solutions below

0
jcx200 On

You need to add an attribute to the class to say what you want the key to be.

I'd assume you would want to use one of the following as the key, so add the key attribute to one of them.

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

    // OR

    [Key]
    public Guid AccountGuid { get; set; }
}