实体类型未定义密钥 [英] Entity Type Has No Key Defined

查看:250
本文介绍了实体类型未定义密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一个实体类型x'没有键定义'问题,但我已经在属性上设置了 [Key] 属性,所以我有点困惑

Another 'Entity Type 'x' has no key defined' question, but I've set the [Key] attribute on a property so I'm a bit confused.

这是我的实体和上下文类:

Here's my entity and context classes:

namespace DoctorDB.Models
{
    public class Doctor
    {
        [Key]
        public string GMCNumber;
        [Required]
        public string givenName;
        [Required]
        public string familyName;
        public string MDUNumber;
        public DateTime MDUExpiry;
        public string MDUCover;
    }

    public class DoctorContext : DbContext
    {
        public DbSet<Doctor> Doctors { get; set; }
    }
}

当我去创建我的控制器时,选择使用该实体和上下文使用Entity Framework方法创建它:

When I go to create my controller, I've selected to create it with the Entity Framework methods using this entity and context:

我收到此错误:

我唯一的想法是, t成功使用[Key]字符串属性。如果你不能那么公平,我会一起努力,但是如果有人可以通过这种方式确认,我将不胜感激。

My only thought is whether you can't successfully use [Key] on a string property. If you can't then fair enough, I'll work round it, but I'd be grateful if someone could confirm this one way or the other.

推荐答案

您需要将 GMCNumber 更改为属性而不是字段。

You need to change GMCNumber to a property not a field.

这篇关于实体类型未定义密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆