Hi all, I've run into a strange issue. Basically, I created my new application called input-outputTest, created my model class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace input_outputTest.Models
{
    public class EmployerDetails
    {
        private string Name { get; set; }
        private string Surname{ get; set; }
        private int Age { get; set; }
        private int EmployerNumber { get; set; }
    }
}

And as I attempted to create a controller called EmployeeController "with read/write actions and view using EF" - screenshot here
add_controller.png
I got an error message saying:
"Unable to retrieve metadata for 'input_outputTest.Models.EmployerDetails'. One or more validation errors were detected during model generation;
-System.Data.Entity.Edm.EdmEntityType::EntityType 'EmployerDetails' has no key defined. Define the key for this Entity Type
-System.Data.Entity.Edm.EdmEntitySet:EntityType:EntitySet EmployerDetails is based on type 'EmployerDetails' that has no key defined"

Screenshot here:
key_error.png

Now, before you say "well then insert the key", please note that I have never had to do that before. Rather, I wonder if there is anything stupid I've done. I was planning to add the controller and then the view, so all I have now is the model. ANy idea?!

Hi

Now, before you say "well then insert the key", please note that I have never had to do that before

This is probably because your previous models used a public property with the name ID or classNameID such as EmployerDetailsID. Entity Framework will discern the primary key from this convention. However, in the model that you have posted above, you do not have such a property name (and also, your properties are set to Private), so you will either need to decorate the property with the Key attribute or provide a property with the name ID or EmployerDetailsID.

HTH

Ah that's it, absolutely right djjeavons, I did have an ID in other projects, but I had absolutely no idea that EF got its primary key from there! Thanks for clarifying that, I will include it. Also, just to clarify something else, isn't it better if the class fields are declared private?

Also, just to clarify something else, isn't it better if the class fields are declared private?

Not necessarily. It depends on their use. Looking at your EmployerDetails class, it makes no sense to have them all declared as private as nothing will be able to access them. I wonder if you are confusing "fields" with properties?

For example, if you were to declare your properties using backing members (fields) then they would indeed be declared as private and only the getters and setters that updated the internal fields would be declared public. However, you are using Auto-Implemented properties that do not contain any logic, so they should be declared public in most cases unless they are for internal use only.

maybe I'm getting confused, you're right, sorry it's that I'm still finding my way around MVC. In any case, thanks for clarifying things, much better now :-)!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.