• 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

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

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.

Link to comment
Share on other sites

  • 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?

Link to comment
Share on other sites

  • 0

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" !!!

Link to comment
Share on other sites

This topic is now closed to further replies.