• 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

    • They've been focusing on security and quality? Could have fooled me. Their own paying customers literally just got breached because they failed to push SharePoint updates downstream to on prem servers operating outside of their "365" ecosystem.
    • The animosity is unnecessary, when I opened the page I only saw one response which never mentioned your other steps, and when I hit reply it jumped straight to the bottom and again, I saw no other responses.  I was simply agreeing with the first comment that said yes, you should be fine if you erase its current operating system. Using another PC, or the copy of Windows that comes on that PC (former option is more trustworthy), download and run the Windows Media Creation tool.  It will walk you thru the process of downloading Windows and writing it to a USB stick.  It will even ask you at one point whether you're reinstalling it to the current machine or installing it on another machine. Then just boot the PC in question from that USB stick.  Usually spamming Esc, Del, F-8, F-9, F-10, F-11, F-12 or F-2 immediately after power on will bring up a boot menu, it varies by manufacturer.  If Windows starts booting you either missed your window or hit the wrong key. Follow the on-screen instructions.  When it gets to the disk formatting part I usually just delete all the partitions on the destination drive, then select the unpartitioned space as my destination.  The Windows installer will then automatically partition the drive as needed. Be prepared to download drivers from the PC manufacturer's website, they may not come bundled with Windows and you may not be able to use things like WiFi or ethernet until you have them.  They "might" work straight away, but they also might not.  Better to be prepared with a spare PC and a USB stick to transfer them over.
    • Wise Disk Cleaner 11.2.5 by Razvan Serea Wise Disk Cleaner is a free disk utility designed to help you keep your disk clean by deleting any unnecessary files. Usually, these unnecessary, or junk files appear as a result of program's incomplete uninstalls, or Temporary Internet Files. It is best if these files are wiped out from time to time, since they may, at some point, use a considerable amount of space on your drives. Wise Disk Cleaner, with its intuitive and easy to use interface, helps you quickly wipe out all the junk files. Using the program is indeed easy. It also works fast when both scanning for files and deleting files. The new Wise Disk Cleaner has more advantages: improved performance, better interface and scans/cleans more thoroughly. Wise Disk Cleaner Free provides lifetime free update service and Unlimited Free technical support. The first Slimming System software Wise Disk Cleaner is the first system slimming tool, which will help you to remove Windows useless files that you don't need, such as Korean IME, Windows Sample music, videos, pictures, Installers and Uninstallers of Updates Patches etc. Wise Disk Cleaner 11.2.5 Build 845 changelog: Added cleaning rules for Legacy Games Launcher, Letasoft Sound Booster, Macrium Reflect, MagicLine4NX, MAGIX Photostory, MakeHuman, Max Recorder, Maxprog iCash, Lexware, LG PC Suite, Lightworks, LINE, Listary, and LockHunter. Improved cleaning rules for Xunlei, PowerToys, Meitu, OneDrive, and Tencent Video. For security reasons, users can no longer delete the latest system restore point in the Restore Center. Enhanced System Slimming. Fixed minor bugs from the previous version. Download: Wise Disk Cleaner 11.2.5 | 6.9 MB (Freeware) Download: Portable Wise Disk Cleaner 11.2.5 | 7.3 MB View: Wise Disk Cleaner Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • I keep getting ignored when I ask what you guys mean by nuke it. I described the steps and I keep getting the same generic instructions. Can you look at what I have posted multiple times already and validate what I have described? You can't assume everyone has your level of expertise and can interpret your nuking advice. After this many posts in this thread, I don't think we need the same generic advice about just nuke it and reinstall. It's already been said. So can you please outline the specifics? Made in Ukraine? Are you sure?
  • Recent Achievements

    • One Year In
      armandointerior640 earned a badge
      One Year In
    • One Month Later
      armandointerior640 earned a badge
      One Month Later
    • One Month Later
      Itbob513626 earned a badge
      One Month Later
    • Week One Done
      Itbob513626 earned a badge
      Week One Done
    • Rookie
      EdwardFranciscoVilla went up a rank
      Rookie
  • Popular Contributors

    1. 1
      +primortal
      615
    2. 2
      ATLien_0
      236
    3. 3
      Xenon
      156
    4. 4
      +FloatingFatMan
      122
    5. 5
      Michael Scrip
      115
  • Tell a friend

    Love Neowin? Tell a friend!