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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:23:15+00:00 2026-05-30T18:23:15+00:00

I’m new to EF, and I’ve just started a new project. I currently have

  • 0

I’m new to EF, and I’ve just started a new project. I currently have a model BrandManagers with one primary key and three foreign keys. I don’t understand how I should map these in the DbContext.

public class BrandManager
{
    public int BrandManagerId { get; set; }

    [Required()]
    public int PersonId { get; set; }

    [Required()]
    public int BrandId { get; set; }

    [Required()]
    public int CountryId { get; set; }

    public virtual Person Person { get; set; }
    public virtual Brand Brand { get; set; }
    public virtual Country Country { get; set; }
}

How will I map this? I have tried the below, to no avail.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Person>()
        .HasRequired(x => x.Country);

    modelBuilder.Entity<Brand>()
        .HasMany(x => x.BrandManagers);

    modelBuilder.Entity<BrandManager>()
        .HasRequired(x => x.Person);
    modelBuilder.Entity<BrandManager>()
        .HasRequired(x => x.Brand);
    modelBuilder.Entity<BrandManager>()
        .HasRequired(x => x.Country);

    base.OnModelCreating(modelBuilder);
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}

I keep getting

Introducing FOREIGN KEY constraint ‘FK_Person_Country_CountryId’ on table ‘Person’ may cause cycles or multiple cascade paths.

I didn’t find any solutions to this specific matter in the introduction tutorials on the ASP.NET website, nor any questions regarding this matter here on SO, so I’m starting to feel like I’m misunderstanding some essential parts of EF.

  • 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-05-30T18:23:16+00:00Added an answer on May 30, 2026 at 6:23 pm

    My answer is more specific to how you map foreign keys in EF Code First. I was about to work on the second part about the multiple cascade paths, when Leniency answered and is correct. So, rather than throwing away my answer, which might be helpful to you, here is the answer to the general how to map foreign keys in EF :). With a bit on the cascading deletes at the bottom.

    You can just use data annotations with no need for mapping in the ModelBuilder. However, if you don’t want your POCO object marked up with database annotations, I have also provided the solution using the modelBuilder.

    Another note, you do not NEED to have PersonId as long as you have the Person reference. In that case, the generator would create the FK for you (and your POCO class would not have Id’s which it does not need). However, it is probably better practice to have the Id for lazy loading (I am not sure if that is why you did this or not).

    Last, you dont even need to set up the Foreign Key if you follow conventions. So, if you have the Country class with a PK of Id, then when you create the Country reference with a CountryId also, EF will look realize through the naming that CountryId is the FK for the country reference. (Source)

    Data Annotations:

    public class BrandManager
    {
        public int BrandManagerId { get; set; }
    
        [Required()]
        public int PersonId { get; set; }
    
        [Required()]
        public int BrandId { get; set; }
    
        [Required()]
        public int CountryId { get; set; }
    
        [ForeignKey("PersonId")]
        public virtual Person Person { get; set; }
        [ForeignKey("BrandId")]
        public virtual Brand Brand { get; set; }
        [ForeignKey("CountryId")]
        public virtual Country Country { get; set; }
    }
    

    DbContext: (Also, in this case, you should not need ANY data annotations on your POCO class because they are all being set up in the modelBuilder)

    modelBuilder.Entity<BrandManager>()
        .HasRequired(x => x.Person)
        .WithMany(x=>x.People)
        .HasForeignKey(x=>x.PersonId);
    

    As to the specific error you are getting, you will need to add .WillCascadeOnDelete(false) as Leniency pointed out to BrandManager->Country. You could remove this for the Person->Country, but it would be better to remove BrandManager because it will ultimately cascade through the Person path anyway.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.