Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Type Has No Key Defined

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; }
    }
}

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

enter image description here

and I get this error:

enter image description here

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.

like image 922
PhilPursglove Avatar asked Jun 09 '11 09:06

PhilPursglove


1 Answers

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

like image 122
codingbadger Avatar answered Oct 02 '22 14:10

codingbadger