0

I don't know if I am doing this right but I basically want to the category names unique in this model

    public class Category
    {
        public int CategoryId { get; set; }

        [Required]
        public string CategoryName { get; set; }

        public string Description { get; set; }

        public List<unitItem> itemList { get; set; }
    }

I tried using an index and added this to my appDbContext

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Category>()
                .HasIndex(b => b.CategoryName).IsUnique();
        }

        public DbSet<Category> Categories { get; set; }

but when I run I get this error:

The entity type 'IdentityUserLogin' requires a primary key to be defined. If you intended to use a keyless entity type call 'HasNoKey()'.'

Any solution to this would be appreciated.

Paul Gudu
  • 363
  • 1
  • 3
  • 10
  • Does this answer your question? [EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType](https://stackoverflow.com/questions/28531201/entitytype-identityuserlogin-has-no-key-defined-define-the-key-for-this-entit) – Magnetron Jul 27 '20 at 16:01

1 Answers1

0

The error:

The entity type 'IdentityUserLogin' requires a primary key to be defined. If you intended to use a keyless entity type call 'HasNoKey()'.'

says you must add a Primary Key to the class 'IdentityUserLogin' which would be unique throughout the table.

 public class Category
 {
    [Key] // <--- add this
    public int CategoryId { get; set; }

    [Required]
    public string CategoryName { get; set; }

    public string Description { get; set; }

    public List<unitItem> itemList { get; set; }
 }

The [Key] data annotation is within the namespace using System.ComponentModel.DataAnnotations; (Press Ctrl + . to import the namespace or type manually)

Dharman
  • 30,962
  • 25
  • 85
  • 135
Rohan Rao
  • 2,505
  • 3
  • 19
  • 39
  • @PaulGudu, You need to build your project > then Add-migrations to make changes in database schema and finally update-database. Unless you add migration and update it, database won't recognize your change and will continue to give the same error. – Rohan Rao Jul 28 '20 at 03:54