c# - 实体类型“任务”需要定义一个主键
问题描述
我正在按照 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
}
}
更新#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)
{
}
}
}
解决方案
我试图在我的最后重现您的代码,我注意到您正在处理的问题是由于 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)
{
}
}
}
问题是由于命名约定中的冲突。我建议将实体名称更改为其他名称,以防止将来发生进一步的冲突。
推荐阅读
- netbeans - Java 应用程序仅在我的计算机中播放选定的视频,而不在其他计算机中播放
- unity3d - 在 Unity 中后处理后的世界到屏幕位置
- google-bigquery - 在 BigQuery 中存储视图所需的最低权限或角色是什么?
- php - 如何使用“amazon-s3-php-class”库在 AWS S3 存储桶中创建新文件夹?
- php - 在 WooCommerce 中向客户订单备注添加自定义结帐字段值
- bash - Sed 无法在远程服务器中使用 sh -c
- mysql - 异步/等待混乱
- css - 卡牌动作悬停时实体化卡牌移动
- android - 如何在 Flutter 中缩小 APK 以使其尽可能小?
- android - 将数据更新到 SQLite-android 的问题