【问题标题】:The type or namespace name 'Keyless' could not be found ef [duplicate]找不到类型或命名空间名称'Keyless' ef [重复]
【发布时间】:2020-04-29 11:50:38
【问题描述】:

我正在使用 .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,为什么会出现此错误?我该如何解决?

谢谢

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2011-06-13
      • 2014-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-12
      • 2011-05-13
      • 2013-03-25
      相关资源
      最近更新 更多