Search code examples
entity-framework-coreentity-framework-6.net-6.0

The entity type required a primary key to be defined - but there is


I probably have a fairly trivial problem with EF configuring 1 table. This is how my class looks like:

public class Task
{
    [Key]
    public int Id { get; set; }

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

    [Display(Name = "Modification Date")]
    public DateTime ModificationDate { get; set; }

    [Required]
    public bool IsDone { get; set; }
}

This is how dbContext looks like:

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions options) : base (options) { }
    public DbSet<Task> Tasks { get; set; }
}

And while creating migration I get this error:

The entity type 'Task' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating' [...]

But as you can see I have an attribute [Key], the property is public and has a setter, what could be the problem?


Solution

  • Ok, that was the dumbest mistake in a long time. It turned out the context was using the Task system class instead of my model class...