Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1615
  • Last Modified:

Can't create strongly typed Controller in MVC

I a new to MVC and am trying to create a Controller based on a model that I defined in a class.  I am using EF and have a model based on a SQL Server database.  I need to get data from an ORACLE database and combine it with the data from the SQL Server database.  I can't do this using a View so I have a class that defines the fields I need from the ORACLE database.  When I try and create a controller based on my model I am getting the following error:

Unable to retrieve Metadata for 'ENL.Models.EmployeeExtract'.  One or more validation errors were detected during model generation:

- System.Data.Entity.EdmEntityType: EntityType 'EmployeeExtract' has no key defined.  Define the key for this EntityTYpe.
- System.Data.Entity.Edm.EdmEntitySet: EntityTYpe: ENtitySet 'Employees' is based on type 'EmployeeExtract' that has no keys defined.

Here is my code:
using System.Web;
using System.Data;
using IntranetSupportTeam_i21Login;
using EmployeeProfileDatabase;
using System.Data.Entity;

namespace ENL.Models
{
    public class EmployeeExtract
    {
        public int profileID { get; set; }
        public string firstname { get; set; }
        public string lastname { get; set; }
        public int locationid { get; set; }
        public string emailaddress { get; set; }
        public string homephone { get; set; }
        public string mobilephone { get; set; }
        public string businessmobilephone { get; set; }
    }

    public class EmployeeExtractDbContext : DbContext
    {
        public DbSet<EmployeeExtract> Employees { get; set; }
    }
}

Open in new window


Any help is greatly appreciated!
0
dyarosh
Asked:
dyarosh
  • 2
1 Solution
 
Paul JacksonCommented:
you need to define the key in the model, if it can't be easily determined which is the key of the model :

using System.Web;
using System.Data;
using IntranetSupportTeam_i21Login;
using EmployeeProfileDatabase;
using System.Data.Entity;

namespace ENL.Models
{
    public class EmployeeExtract
    {
        [Key]
        public int profileID { get; set; }
        public string firstname { get; set; }
        public string lastname { get; set; }
        public int locationid { get; set; }
        public string emailaddress { get; set; }
        public string homephone { get; set; }
        public string mobilephone { get; set; }
        public string businessmobilephone { get; set; }
    }

    public class EmployeeExtractDbContext : DbContext
    {
        public DbSet<EmployeeExtract> Employees { get; set; }
    }
} 

Open in new window

0
 
Paul JacksonCommented:
The normal configuration of the key is classnameID, so in your case it would be EmployeeExtractID, or you can have just ID. In these cases you wouldn't have to specify the [Key] attribute.
0
 
dyaroshAuthor Commented:
Thank you.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now