繁体   English   中英

实体类型“任务”需要定义一个主键

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

我正在按照 aspnetboilerplate.com 关于使用他们的框架进行开发的教程进行培训。 我被困在第一个编码点,我必须创建一个基本表"Task" ,如下面的代码中所述。

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

我也在我的 WebApp DBContext中添加了以下代码。

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

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

    }
}

本教程没有提到有关此代码的任何错误,但每次我发出命令时

Add-migration "Initial"

在 package 管理器控制台中,我收到此错误。

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

我浏览了 web 的类似错误,我发现的每个解决方案都对我不起作用......

更新#1:我将代码编辑为此,但错误仍然存在。

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

教程链接: https://aspnetboilerplate.com/Pages/Documents/Articles/Introduction-With-AspNet-Core-And-Entity-Framework-Core-Part-1/index.html

更新#2:这是 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)
        {

        }
    }
}

我试图在我的最后重现您的代码,我注意到您正在处理的问题是由于上下文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)
        {

        }
    }
}

问题是由于命名约定中的冲突。 我建议将实体名称更改为其他名称,以防止将来发生进一步的冲突。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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