簡體   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