我正在接受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
}
}
我在WebAppDBContext
中也添加了以下代码。
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)
{
}
}
}
我试图复制你的代码在我的结束,我注意到,你正在处理的问题是由于错误的命名空间在上下文中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)
{
}
}
}
问题是由于命名约定中的冲突造成的。我建议将实体的名称更改为其他名称,以防止将来发生进一步的冲突。