Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primary key not found when scaffolding controller in MVC core

When trying to scaffold a controller I get the following error:

"There was an error running the selected code generator: 'The entity type 'Company.Models.Office' requires a primary key to be defined at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.b_6-0()"

I am new to MVC Core but my understanding is that a primary key is defined by default as ID or classnameID. I have also tried adding the [Key] attribute and renaming the primary key to OfficeID. I have gone through a similar process with another class and did not run into this problem

public class Office
    {
        public int ID;
        public string Name;
        public int SiteID;

    }
like image 790
coolhand Avatar asked Aug 02 '17 18:08

coolhand


1 Answers

I'm not sure if this will resolve your issue, but maybe try the following. It sounds similar to what you described already, however I've posted it anyway just to be sure.

public class Office
{
    [Key]
    public int OfficeID { get; set; }
    public string Name { get; set; }
    public int SiteID { get; set; }
}

You should be aware that it doesn't really matter what your primary key is called. However, for easiness and following standards, you should call it either 'ID' or the table/class name plus 'ID'.

Also, have you tried restarting Visual Studio and your PC? Sometimes I find that this is the only way to resolve some compile time errors.

like image 124
user1779775 Avatar answered Nov 09 '22 22:11

user1779775