首页 > 解决方案 > 实体类型“任务”需要定义一个主键

问题描述

我正在按照 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"

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

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

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

更新#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)
        {

        }
    }
}

标签: c#entity-framework-coreaspnetboilerplate

解决方案


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

        }
    }
}

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


推荐阅读