c# - 找不到类型或命名空间名称 'Keyless' ef

标签 c# model-view-controller entity-framework-core

这个问题在这里已经有了答案:





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/

相关文章:

ruby-on-rails - 在 View 模板中保留逻辑是否可以接受?

c# - EF 核心 3 : Configure backing field of navigation property

stored-procedures - EF Core 2.0 如何使用 SQL 存储过程

c# - 让 Rotativa PDF 在共享主机上工作

c# - Assembly.LoadFrom 如何解决非托管依赖项?

c# - 将 HQL 与标准混合

c# - WPF 中的数独 - 我应该为表格使用什么基本元素?

c# - Windows Mobile 用户界面架构

c# - 如何使 EF 核心将自定义排序转换为纯 'When Then' 而不是分层

c# - 将 Xamarin 连接到 Virtual Box