• 0

Setting up Navigation Properties and retrieving info in Entity Framework and MVC 5


Question

Hello neowin community! I know many badasses over here that can help me through my problem. I haven't fully understood how does Lazy loading works in Entity Framework. In fact, I haven't understood how to work with Navigation Properties, Foreign Keys, using Entity Framework at all. I haven't found a tutorial which explains me baby-step by baby-step how is it achieved. So I'm just jerking off code (something I don't like doing).

 

I currently have the following model (Using ASP.NET MVC 5 + Entity Framework 6)

namespace WorldCrowns.Models
{
    /*
     * This is the Contacts Model. This code shall contain all the models for all the friends 
     * */
    public class Contacts
    {
        [HiddenInput(DisplayValue=false)]
        [Key]
        public int ContactsID { get; set; }
        [HiddenInput(DisplayValue=false)]
        public int IboID { get; set; }
        public string Name { get; set; }
        
        [Display(Name="First Time Met")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public string FirstMeet { get; set; }
        public string Phone { get; set; }
        
        [Display(Name="Date of Contact")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime? DateOfContact { get; set; }
        
        public string Comments{ get; set; }
        
        [Display(Name="Next Exposure")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime? NextExposure { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        [Display(Name = "Next Exposure 2")]
        public DateTime? NextExposure2 { get; set; }

        //This is analogue to SelectedQ from Ibo Model
        //The main purpose is to receive the incoming data from the SelectList
        [NotMapped]
        public string SelectedResult { set; get; }

        [Display(Name = "Result")]
        public int ExposureResultID { get; set; }
        public virtual ICollection<ExposureResults> ExposureResult {get; set;}
        //public virtual ICollection<ExposureResults> ExposureResults { get; set; }
        public virtual ICollection<Ibo> Ibo { get; set; }
    }

    public class ExposureResults
    {
        public int ExposureResultsID { get; set; }
        public string ExposureResult { get; set; }
     }
}

so here it is.

 

So, what do I want to achieve?

Easy:

Perform a modified CRUD operation

 

Create a new "Contact" using a dropdown list from the ExposureResults class (which will have data predefined), and read it back. I got the dropdown working, what I'm stuck is making the relation work, so when I get the "ExposureResultID" property, I grab the "ExposureResult" property from ExposureResults Table.

 

 

I've tried to access it like this:

@model IEnumerable<WorldCrowns.Models.Contacts>

@foreach (var item in Model) {
   //I've tried accessing like these:
  //#1

  model.ExposureResultID

  //#2
   model.ExposureResult.ExposureResultsID

   #3
   model.ExposureResult;

   #4
   model.ExposureResult.ExposureResult

}

And haven't managed to get the correct display.

 

Any ideas?

 

Thanks a million! :D

3 answers to this question

Recommended Posts

  • 0
  On 12/10/2014 at 11:17, Riva said:

So if I am understanding correctly, you have a contact that has multiple exposure results. The property Contact.ExposureResultID is not relevant to a one to many relationship.  ExposureResult is the one to use.

Thanks!

 

What I'm trying to say in here is that a "Contact" will have one "ExposureResult" It's a one to one relationship.

  • 0

Riva, I've managed to do as you've said :)

 

I am grabbing now the ID of the row, but not the actual column I want.

 

When I call model.ExposureResult it gives me the ID, how can I do to call the "ExposureResult" column from the "ExposureResult" table?

  • 0
  On 14/10/2014 at 01:12, Jose_49 said:

Thanks!

 

What I'm trying to say in here is that a "Contact" will have one "ExposureResult" It's a one to one relationship.

Sorry.

 

It's a one to many relationship, going from the "ExposureResult" to "Contact" !!!

This topic is now closed to further replies.
  • Posts

    • Basically, they can hire someone with less skill for a lower wage, for example from India, and use AI to bring that employees output up to the level of someone more experienced and expensive. It’s a broad cost-cutting strategy, and plenty of companies are already doing it. With AI involved, the employee just needs to check that the AI's results aren’t complete junk. They don’t actually have to know how to produce the work themselves. You just get one high level employee to triple check who does. The workflow looks like this: AI generates the content, a low-skilled worker filters out anything clearly broken or wrong, and a high-skilled worker gives it a final pass. Instead of hiring a full team of experts, companies can rely on a few at the top while AI and cheaper labour handle the bulk of the process.
    • Aah, always the evil terrorist group. Why not just the Russians, they're worse than any terrorist organisation.
    • LG Electronics sees profit dip but boosts shareholder returns by Paul Hill LG Electronics (LGE) has reported consolidated sales of 20.74 trillion Korean Won (approximately $15.14 billion USD) and an operating profit of 639.4 billion Korean Won (approximately $466.75 million USD) for the second quarter of 2025. The firm saw both its revenue and operating profits decline year-over-year due to external factors such as US tariff policies, ongoing geopolitical issues in the Middle East, and a slowdown in consumer spending. While the company has faced challenges, the company is aiming for what it called qualitative growth by strengthening its subscription services, direct online sales, and business-to-business (B2B) segments. Key areas of growth for LGE include automotive electronics, heating, ventilation, and air conditioning (HVAC) systems, and the webOS platform. The US tariffs are a major headache for companies around the world, given the size of the market there and President Trump's demands for bringing manufacturing to the States. To combat the rising US tariffs, LGE is trying to optimize global production and refine its market-specific approaches for premium and mass-market products. While the company’s profits dipped, it announced an interim dividend of 500 Korean Won (approximately $0.37 USD) per share for both common and preferred, with another payout later in the year that should take the total for 2025 to at least 1,000 Korean Won (approximately $0.73 USD). You must be holding the stock on August 8, 2025, to get the payout on August 22, 2025. The firm also said it will cancel 761,427 common treasury shares on July 31, 2025, making shares more scarce which could increase the value of remaining shares for investors. In terms of LGE’s various business divisions, things were mixed. Its Home Appliance Solution (HS) business achieved year-over-year sales growth, maintaining profitability, thanks to a dual strategy for premium and volume segments and growth in online sales and subscriptions. Its Media Entertainment Solution (MS) business experienced declines in sales and operating profits with the latter turning negative due to market uncertainties and stiff competition. Its Vehicle Solution (VS) business showed growth in sales and operating profit thanks to increased orders from European automotive makers. Finally, its Eco Solution (ES) business saw domestic sales increase, but overseas sales growth was limited due to US tariffs. This led to a slight year-over-year drop in operating profit due to higher costs. Image via Depositphotos.com
    • "Microsoft plans to remove the MSN feed..." Yaaay, finally the horrible dumpster fire of badly translated tabloid trash is going away! Good riddance! "...and replace it with Copilot Discover..." Oh ffs...
    • Similar to how Nvidia stopped caring about the consumer market a while ago and now just pays lip service to it because it prints money.
  • Recent Achievements

    • Week One Done
      Itbob513626 earned a badge
      Week One Done
    • One Month Later
      Itbob513626 earned a badge
      One Month Later
    • Rookie
      EdwardFranciscoVilla went up a rank
      Rookie
    • Week One Done
      MoJo624 earned a badge
      Week One Done
    • Collaborator
      aeganwn earned a badge
      Collaborator
  • Popular Contributors

    1. 1
      +primortal
      618
    2. 2
      ATLien_0
      243
    3. 3
      Xenon
      159
    4. 4
      Michael Scrip
      126
    5. 5
      +FloatingFatMan
      122
  • Tell a friend

    Love Neowin? Tell a friend!