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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:56:50+00:00 2026-06-04T15:56:50+00:00

Using EF Code First I have an model object that has multiple properties that

  • 0

Using EF Code First I have an model object that has multiple properties that are associated to a single model object:

public class Job 
{
    public int Id { get; set; }
    public int Company1Id { get; set; }
    public int Company2Id { get; set; } // References CompanyId
    public int Company3Id { get; set; } // References CompanyId
    ...

    public virtual Company Company1Info { get; set; }
    public virtual Company Company2Info { get; set; }
    public virtual Company Company3Info { get; set; }
 }

 public class Company
 { 
    public int CompanyId { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
 }

Relationships in JobMap class

        this.HasRequired(t => t.Company1Info)
            .WithMany()
            .HasForeignKey(d => d.Company1Id);
        this.HasRequired(t => t .Company2Info)
            .WithMany()
            .HasForeignKey(d => d.Company2Id);
        this.HasRequired(t => t.Company3Info)
            .WithMany()
            .HasForeignKey(d => d.Company3Id);

Method in my repo to get data

 public Job GetById(int id)
    {
        return _dbContext.Set<Job>()
            .Include(t => t.Company1Info)
            .Include(t => t.Company2Info)
            .Include(t => t.Company3Info)
            .First(x => x.Id == id);
    }

When I run the application the Company2Info and Company3Info are null. I tried setting up a new DbSet instance for each company in the context but I got an Unsupported Exception.

Thanks!

Update: Here is an answer for the same problem but this is not working for me Entity Framework Code First – two Foreign Keys from same table

  • 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-04T15:56:51+00:00Added an answer on June 4, 2026 at 3:56 pm

    Solution:

    The setup I have in my question and the solution in the link are correct. The object models I was creating instances of were null due to a non-existent key — in other words, the Id for the company I was trying to reference did not exist.

    The only change I made was to add the WillCascadeOnDelete method and set it to false as the default is true — note I’m using the Fluent API in the JobMap class and not the Context ModelBuilder.

    Relationships in JobMap class

        this.HasRequired(t => t.Company1Info)
            .WithMany()
            .HasForeignKey(d => d.Company1Id)
            .WillCascadeOnDelete(false);
        this.HasRequired(t => t .Company2Info)
            .WithMany()
            .HasForeignKey(d => d.Company2Id)
            .WillCascadeOnDelete(false);
        this.HasRequired(t => t.Company3Info)
            .WithMany()
            .HasForeignKey(d => d.Company3Id)
            .WillCascadeOnDelete(false);
    

    HTH

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

Sidebar

Related Questions

Using Entity Framework 4 and code first how would I create a model that
I have written a checkers game model which has a Board object that holds
I have an AXIS2/JAX-WS web service using a code first implementation (yes I know,
I'm using Entity Framework Code First v 4.3.0 I have a two entities with
I have a table called Tournaments which is created using the EF code first
We are using EF 4.0 with code first approach. I have defined constraint for
I have been using this code with great success to pull out the first
When defining a string in a class using Code First, the default for the
In this code, I have an Entity Framework 4 model with a single Thing
I'm using ASP.NET MVC 3 code-first and I have added validation data annotations to

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.