找不到类型或命名空间名称“Keyless” ef

Jes*_*sus 5 c# model-view-controller entity-framework-core

我正在使用 .Net Core 3,采用 ef 模型优先方法。我想创建一个没有主键的类,所以我使用了这个:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace Beyond.Models
{

    public class Bab
    {
       [Keyless]
       public DateTime DateTime { get; }
       }
}
Run Code Online (Sandbox Code Playgroud)

但我有这个错误:

错误 CS0246 找不到类型或命名空间名称“Keyless”(您是否缺少 using 指令或程序集引用?)

如果我声明 Microsoft.EntityFrameworkCore,为什么会出现此错误?我该如何解决?

谢谢

Tim*_*Tim 0

Keyless 注释是在 EFCore 5.0 中添加的,因此如果使用 5.0 之前的 EFCore 版本,则会出现程序集未找到编译错误。

https://learn.microsoft.com/en-us/ef/core/modeling/keyless-entity-types?tabs=data-annotations

我相信Keyless注释也必须在类上而不是在字段上。