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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:07:43+00:00 2026-06-08T11:07:43+00:00

I have a scenario I’m getting a little muddled with using EF code first.

  • 0

I have a scenario I’m getting a little muddled with using EF code first. The classes I’ve created are below:

public class Company
{
    public int Id { get; set; }
    public List<Contact> Contacts { get; set; }
    public List<Job> Jobs { get; set; }
}

public class Contact
{
    public int Id { get; set; }
    [ForeignKey("CompanyId")]
    public virtual Company Company { get; set; }
    public int CompanyId { get; set; }       
    public List<Job> Jobs { get; set; }
}

public class Job
{
    public int Id { get; set; }
    [ForeignKey("CompanyContactId")]
    public virtual CompanyContact CompanyContact { get; set; }
    public int CompanyContactId { get; set; }
    [ForeignKey("CompanyId")]
    public virtual Company Company { get; set; }
    public int CompanyId { get; set; } 
}    

However, when I build the DB I get the following error:

Introducing FOREIGN KEY constraint ‘FK_Contacts_Company_CompanyId’ on table ‘Contacts’ may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

So a little research indicates the answer to this is to use the Fluent API to define the mappings as required but I can’t get my head around how to do this or find an example of a similar scenario.

I realise I could remove the Company class from Job and navigate through Contact but I’d prefer not to if possible.

Any help gratefully received

  • 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-08T11:07:45+00:00Added an answer on June 8, 2026 at 11:07 am

    You want to use the EF model builder to set up these relationships.

    An example of how you would do this for one of your properties would be the following:

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Contact>().HasOptional(e => e.Company).WithMany(c=>c.Contacts);
        } 
    

    For more of an explanation around how to use the modelbuilder take a look at my article on EF Navigation Properties

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

Sidebar

Related Questions

I have this scenario well, i'll let the model explain. public class ScheduleMonthlyPerDayModel {
I have this scenario: One custom class (Customer) with some properties, like this: public
I have scenario like Here is the abstract class abstract class A { public
we are using Struts2 in our code and I have scenario where I need
I have scenario, in which I am getting images using Web Service and all
Have this scenario: public class Base { public string Name; } public Class ClassA
I have some scenario like below : /** * @ORM\Entity * @ORM\Table(name=role) */ class
I have a scenario where people login to the IM we developed using the
I have a scenario where I have a div bound to an observable using
I have a scenario with code like the following: extracted_data = data.map{|row| ((row.some_long_number.to_f) if

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.