Solved

Can't create strongly typed Controller in MVC

Posted on 2014-02-05
4
1,423 Views
Last Modified: 2014-02-23
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
Comment
Question by:dyarosh
  • 2
4 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
ID: 39838647
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
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 39838658
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
 

Author Comment

by:dyarosh
ID: 39838689
Thank you.
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What namespace do I need to import? 2 29
jquery progress bar 3 42
VB.net Move a class from Solution Items to a project 2 23
How come this regular expression allows "e"? 14 38
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question