[英]ASP.NET Boilerplate - Entity Task Requires Primary Key
Im just New to C# and Currently I am trying to learn ASP.NET Single Web Page Application.我只是 C# 的新手,目前我正在尝试学习 ASP.NET 单 Web 页面应用程序。
I'm currently trying to This Tutorial that is provided by ASP.NET Boilerplate, But I am having issues when I am trying to run Add-Migration "Initial"
我目前正在尝试 ASP.NET 样板提供的本教程,但是当我尝试运行Add-Migration "Initial"
时遇到问题
Which says: "The entity type 'Task' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. For more information on keyless entity types, see https://go.microsoft.com/fwlink/?linkid=2141943."其中说: “实体类型'Task'需要定义一个主键。如果您打算使用无键实体类型,请在'OnModelCreating'中调用'HasNoKey'。有关无键实体类型的更多信息,请参阅https:// go.microsoft.com/fwlink/?linkid=2141943。”
This is my Tasks.cs这是我的Tasks.cs
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Abp.Domain.Entities;
using Abp.Domain.Entities.Auditing;
using Abp.Timing;
namespace Acme.SimpleTaskApp.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
}
}
This is my SimpleTaskAppDbContext.cs这是我的SimpleTaskAppDbContext.cs
using Abp.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;
namespace Acme.SimpleTaskApp.EntityFrameworkCore
{
public class SimpleTaskAppDbContext : AbpDbContext
{
//Add DbSet properties for your entities...
public DbSet<Task> Tasks { get; set; }
public SimpleTaskAppDbContext(DbContextOptions<SimpleTaskAppDbContext> options)
: base(options)
{
}
}
}
I don't know what else am I missing, I tried different tutorials that I can explore with CRUD while using asp.net boilerplate.我不知道我还缺少什么,我尝试了不同的教程,我可以在使用 asp.net 样板时使用 CRUD 进行探索。
Your Entity is missing a Primary Key
.您的实体缺少Primary Key
。 If you add this to your Entity it should work.如果您将此添加到您的实体中,它应该可以工作。
[Key]
public int Id { get; set; }
Further Information https://docs.microsoft.com/en-us/ef/core/modeling/keys?tabs=data-annotations更多信息https://docs.microsoft.com/en-us/ef/core/modeling/keys?tabs=data-annotations
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.