简体   繁体   中英

The entity type "Task" requires a primary key to be defined

I'm training following aspnetboilerplate.com tutorials about developing using their frameworks. I'm stuck at the very first coding point where I have to create a basic table "Task" as stated in the code below.

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Abp.Domain.Entities;
using Abp.Domain.Entities.Auditing;
using Abp.Timing; 

namespace WebApp.Tasks
{
    [Table("AppTasks")]
    public class Task : Entity, IHasCreationTime
    {
        public const int MaxTitleLength = 256;
        public const int MaxDescriptionLength = 64 * 1024; //64KB

        [Required]
        [StringLength(MaxTitleLength)]
        public string Title { get; set; }

        [StringLength(MaxDescriptionLength)]
        public string Description { get; set; }

        public DateTime CreationTime { get; set; }

        public TaskState State { get; set; }

        public Task()
        {
            CreationTime = Clock.Now;
            State = TaskState.Open;
        }

        public Task(string title, string description = null)
            : this()
        {
            Title = title;
            Description = description;
        }
    }

    public enum TaskState: byte
    {
        Open = 0,
        Completed = 1
    }
}

I added the following code in my WebApp DBContext , too.

public class WebAppDbContext : AbpDbContext
{
    public DbSet<Task> Tasks { get; set; } //<- This line

    public WebAppDbContext(DbContextOptions<WebAppDbContext> options) 
        : base(options)
    {

    }
}

The tutorial does not mention any error regarding this code, but every time I make the command

Add-migration "Initial"

in the package manager console, I get this error.

The entity type "Task" requires a primary key to be defined.

I surfed the web for similar errors and each solution I've found does not work for me...

Update #1: I edited the code to this, but the error still remains.

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Abp.Domain.Entities;
using Abp.Domain.Entities.Auditing;
using Abp.Timing;

namespace WebApp.Tasks
{
    [Table("AppTasks")]
    public class Task : Entity, IHasCreationTime
    {
        public const int MaxTitleLength = 256;
        public const int MaxDescriptionLength = 64 * 1024; //64KB

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

        [Required]
        [StringLength(MaxTitleLength)]
        public string Title { get; set; }

        [StringLength(MaxDescriptionLength)]
        public string Description { get; set; }

        public DateTime CreationTime { get; set; }

        public TaskState State { get; set; }

        public Task()
        {
            CreationTime = Clock.Now;
            State = TaskState.Open;
        }

        public Task(int id, string title, string description = null)
            : this()
        {
            Id = id;
            Title = title;
            Description = description;
        }
    }

    public enum TaskState: byte
    {
        Open = 0,
        Completed = 1
    }
}

Tutorial link: https://aspnetboilerplate.com/Pages/Documents/Articles/Introduction-With-AspNet-Core-And-Entity-Framework-Core-Part-1/index.html

Update #2: This is the code of WebAppDbContext.cs

using Abp.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;

namespace WebApp.EntityFrameworkCore
{
    public class WebAppDbContext : AbpDbContext
    {
        //Add DbSet properties for your entities...

        public DbSet<Task> Tasks { get; set; }

        public WebAppDbContext(DbContextOptions<WebAppDbContext> options) 
            : base(options)
        {

        }
    }
}

I've tried to reproduce your codes on my end and I noticed that the problem that you're dealing with is due to the wrong namespaces in the context WebAppDbContext .

using Abp.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
//using System.Threading.Tasks;//<------------ this line causes the error

using WebApp.Tasks; //<----------- You need to add this namespace.

namespace WebApp.EntityFrameworkCore
{
    public class WebAppDbContext : AbpDbContext
    {
        //Add DbSet properties for your entities...

        public DbSet<Task> Tasks { get; set; }

        public WebAppDbContext(DbContextOptions<WebAppDbContext> options) 
            : base(options)
        {

        }
    }
}

The problem is due to a conflict in the naming convention. I would recommend changing the name of the entity to something else to prevent further conflicts in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM