Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9214385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:57:50+00:00 2026-06-18T01:57:50+00:00

I an fairly new to MVC, and have created an MVC4 application using EF-database-first.

  • 0

I an fairly new to MVC, and have created an MVC4 application using EF-database-first. The database does not contain foreign key definitions and I can’t add them (I don’t own the database). Here are two example classes from the database:

public partial class Allocation
{
    public int AllocID { get; set; }
    public int DeptID { get; set; }
    public decimal AllocationPercent { get; set; }
}

public partial class Department
{
    public int DeptID { get; set; }
    public string DeptName { get; set; }
    public string Account { get; set; }
}

The default Allocation Index page shows the department ID. I want to show the department name instead. How can I do this without navigation properties?

I tried

public class AllocationController : Controller
{
    private Entities db = new Entities();

    //
    // GET: /Allocation/

    public ActionResult Index()
    {
        return View(db.Allocation.Include(d => d.DeptID).ToList());
    }
...

but this gives an error (“A specified Include path is not valid. The EntityType ‘TESTModel.Allocation’ does not declare a navigation property with the name ‘DeptID’.”)…

I’m not sure how to code eager-loading or explicit-loading without navigation properties either, which prompted this question. Efficiency-wise, I don’t believe it matters which way I load the related information, so any help in any direction would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:57:51+00:00Added an answer on June 18, 2026 at 1:57 am

    The database does not have to have definitions, as long as the fields are there and the entities have been placed in the database with referential integrity in mind. All you need to do is let entity framework know about the relationship. This is done with the virtual keyword to create “Navigational Properties”.

    public partial class Allocation
    {
     public int AllocID { get; set; }
     public int DeptID { get; set; }
     public decimal AllocationPercent { get; set; }
     public virtual Department Department { get; set; } /* this is your nav property */
    }
    
    public partial class Department
    {
     public int DeptID { get; set; }
     public string DeptName { get; set; }
     public string Account { get; set; }
    }
    

    Now you can do:

    db.Allocation.Include(a => a.Department).ToList()
    

    There may be an error which requires you to use a foreign key definition (although I do not think so). If this is the case, you will need to decorate your navigation property like this

    [ForeignKey("DeptID")]
    public virtual Department Department { get; set; }
    

    You may also try it this way:

     public int AllocID { get; set; }
     [ForeignKey("Department")]
     public int DeptID { get; set; }
     public decimal AllocationPercent { get; set; }
     public virtual Department Department { get; set; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a new MVC 4 Application with a fairly basic View/Controller. The associated
First off, I'm fairly new to MVC but have an existing project in MVC3
Hi fairly new to using MVC radio buttons. I have some code: @Html.RadioButtonFor(modelItem =>
I’m fairly new at C# and MVC and have used lambdas on certain occasions,
I'm fairly new to ASP MVC so go easy :). I have a form
I am fairly new to MVC but not sure exactly which Redirect... replaces the
I am fairly new at using the ASP.NET MVC framework and was hoping that
Being fairly new to MVC 3 I am not sure of the best approach
I've fairly new to PHP MVC and I'm not 100% sold I'm doing this
I am fairly new to MVC. I have a simple problem. I have 3

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.