这个问题在这里已经有了答案:
Entity Framework - [Keyless] Data Annotation Missing
(3 个回答)
去年关闭。
我正在使用 .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; }
}
}
但我有这个错误:
错误 CS0246 找不到类型或命名空间名称“Keyless”(您是否缺少 using 指令或程序集引用?)
如果我声明 Microsoft.EntityFrameworkCore,为什么会出现此错误?我该如何解决?
谢谢
最佳答案
Keyless 注释是在 EFCore 5.0 中添加的,因此如果使用 EFCore 5.0 之前的版本,则会出现程序集未找到编译错误。
https://docs.microsoft.com/en-us/ef/core/modeling/keyless-entity-types?tabs=data-annotations
我相信 Keyless 注释也必须在类里面而不是在场上。
关于c# - 找不到类型或命名空间名称 'Keyless' ef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61501146/