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 8122705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:54:59+00:00 2026-06-06T05:54:59+00:00

I have a situation where i have classes JobDescription and Image . I want

  • 0

I have a situation where i have classes JobDescription and Image . I want to establish the relation of one to zero or many. If JobDescription exists it may have collection of images or may not have any image at all. Same goes with Image and ImageSection. With my classes am i doing right?

public class JobDescription
        {
            public int JobDescriptionID { get; set; }

           // Other attributes and navigations


            public int? ImageID { get; set; }

            [ForeignKey("ImageID")]
            public virtual List<Image> Image { get; set; }

         }

      public class Image
        {
            public int ImageID { get; set; }

            public string ImageName { get; set; }

            public int? ImageSectionID { get; set; }

            [ForeignKey("ImageSectionID")]
            public virtual ImageSection ImageSection { get; set; }


        }
  • 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-06T05:55:01+00:00Added an answer on June 6, 2026 at 5:55 am

    The foreign key property belongs to the “one” side of the relationship, not to the “many” side. Your model classes should look like this:

    public class JobDescription
    {
        public int JobDescriptionID { get; set; }
    
        // Other attributes and navigations
    
        public virtual List<Image> Image { get; set; }
    }
    
    public class Image
    {
        public int ImageID { get; set; }
    
        public string ImageName { get; set; }
    
        public int? JobDescriptionID { get; set; }
    
        [ForeignKey("JobDescriptionID")]
        public virtual JobDescription JobDescription { get; set; }
    }
    

    And the same kind of relationship for ImageSection. If an image always must have a JobDescription the JobDescriptionID in Image must be of type int, not int?. In this example the ForeignKey attribute is not necessary because EF will recognize the FK property by naming conventions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a one-to-many relationship between two classes for this situation. I have a
First I want to describe my situation briefly. I have two classes, one MainClass
I have the following situation: Three concrete service classes implement a service interface: one
The situation: I have multiple classes that should each hold a variable with a
Consider a situation. I have an in-proc COM server that contains two COM classes.
Ok so I'm doing a simulation about a Mall I have many classes in
I have a situation where I have have a lot of model classes (~1000)
I have a situation where I have 4-5 very similar classes which I'd like
I have a situation where I have a couple of case classes where all
I have a situation in my code, where I cannot clean up my classes

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.