2
votes

I am new to ASP.NET MVC, I am trying to learn, but I have the following error:

"Unable to retrieve metadata for 'AEMOS.Models.Proyects'. One or more validation errors were detected during model generation:

Proyects: : EntityType 'Proyects' has no key defined. Define the key for this EntityType.

Proyects: EntityType: EntitySet 'Proyects' is based on type 'Proyects' that has no keys defined.

I have searched Google for this error, even here in StackOverflow but cant reach a success.

This is my class:

using System;
using System.ComponentModel.DataAnnotations;
using System.Globalization;

namespace AEMOS.Models
{
    public class Proyects
    {
        [Key]
        public int ProyectsId { get;set;}
        public string Name { get; set; }
        public int Year { get; set; }
        public Boolean Active = false;


    }
}

I selected MVC 5 Controller with views, using Entity Framework for the controller creation. On the next window I selected the Model Proyects, Data Context ApplicationDbContext and the selected layout.

I have tried multiple class configurations (without [Key], with Key name Id instead of ProyectsId, ...).

Do you know whats happening?

1
Did you compile the application? - CodeCaster
OH!!! Thats it, sorry If I am a rookie, didn't read anything about compile. Thanks a lot to everyone. Can you post as an answer to mark as done please? - Programador Adagal

1 Answers

3
votes

I'm fairly sure this has been answered before, but I couldn't find the duplicate.

The scaffolding wizard, which generates controllers and views based on an Entity Framework entity type, looks at your binaries, not your code.

So you need to compile (build) the assembly containing the entity types after making changes for the wizard to pick up these changes, such as adding a Key attribute.