非实体 Class 需要 [Key] 属性

Non-Entity Class Requiring [Key] Attribute

我收到 EntityType 'ScheduleNote' has no key defined. Define the key for this EntityType. 的 POCO,与 Entity Framework 无关。 EF 是否要求我为项目中的每个 class 定义一个 [Key],无论它是否处理数据库?

更新:

这是 ScheduleNote 代码:

using System;

namespace Domain
{
    public class ScheduleNote
    {
        public string NoteId { get; set; }
        public NoteTypeEnum Type { get; set; }
        public string TypeFK { get; set; }
        public string UserId { get; set; }
        public string AuthorName { get; set; }
        public string Subject { get; set; }
        public string Message { get; set; }
        public DateTime? StartDate { get; set; }
        public DateTime? EndDate { get; set; }
        public DateTime InsertDate { get; set; }
        public DateTime? DeleteDate { get; set; }
        public bool IsPublic { get; set; }
        public bool IsModified { get; set; }
        public bool IsDeleted { get; set; }
    }

    public enum NoteTypeEnum
    {
        INSIGHT_EVENT = 0,
        INSIGHT_SERIES = 1,
        INSIGHT_TEAM = 2
    }
}

因此,当应用程序启动时 (ASP.NET Web API),第一次使用 EF 调用数据库(与此 class 无关)错误"no key defined" 错误。 class 未在我的 DbContext 或任何地方定义。它只存在于项目中,EF 不喜欢它没有 [Key] 属性。

我可以只添加属性并继续,但这并不能解释 "why" 我需要这样做。我的理解是,引入 EF 后不必更改新代码或遗留代码。

由于您以某种方式将 Class 用作相关实体,因此您需要使用 属性 上的 [NotMapped] 属性让 EF 知道您没有希望它自动连接 属性。或者干脆将其删除作为参考。