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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:15:37+00:00 2026-05-26T13:15:37+00:00

I have a simple 1:many aggregate relationship, lets say: public class Parent { public

  • 0

I have a simple 1:many aggregate relationship, lets say:

public class Parent
{
    public string Name {get; set;}
    public Child SelectedChild {get; set;}
    public Child PublishedChild {get; set;}
    public virtual ICollection<Child> AllChildren {get; set;}
}

public class Child
{
    public string Name {get; set;}
    [Required]
    public Parent Father {get; set;}
}

When creating the schema from this model I get the error:

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

So I add the following to OnModelCreating:

        modelBuilder.Entity<Child>()
            .HasRequired(v => v.Parent)
            .WithOptional(c => c.SelectedChild)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<Child>()
            .HasRequired(v => v.Parent)
            .WithOptional(c => c.PublishedChild)
            .WillCascadeOnDelete(false);

This gets round the original error but I now get:

Unable to determine the principal end of the ‘xxx.Parent_SelectedChild’ relationship.
Multiple added entities may have the same primary key.

Can anyone help?
All I essentially want to do is refer to particular child records on a 1:many aggregate relationship from the parent. I assume EF will create INT child id columns on the parent called e.g. SelectedChild_Id & PublishedChild_Id (or similar).

Thanks in advance
-macon

Edit: In response to @Slauma:
I can get a schema generated using:

            modelBuilder.Entity<Parent>()
            .HasOptional(p => p.SelectedChild)
            .WithOptionalPrincipal()
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<Parent>()
            .HasOptional(p => p.PublishedChild)
            .WithOptionalPrincipal()
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<Parent>()
            .HasMany(p => p.AllChildren)
            .WithRequired(c => c.Father)
            .WillCascadeOnDelete(false);

But this generates multiple FK on the Child record e.g. Parent_Id, Parent_Id1. I just want a reference from the Parent to one of the child rows e.g. Parent_SelectedChildId. Do I have to do this manually with an int column on parent?

  • 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-26T13:15:37+00:00Added an answer on May 26, 2026 at 1:15 pm

    I think you have three 1-to-many relationships:

    modelBuilder.Entity<Parent>()
        .HasOptional(p => p.SelectedChild)
        .WithMany()
        .WillCascadeOnDelete(false);
    
    modelBuilder.Entity<Parent>()
        .HasOptional(p => p.PublishedChild)
        .WithMany()
        .WillCascadeOnDelete(false);
    
    modelBuilder.Entity<Parent>()
        .HasMany(p => p.AllChildren)
        .WithRequired(c => c.Father)
        .WillCascadeOnDelete(false);
    

    Edit

    I’ve tested my mapping above with exactly the Parent and Child class you provided in your question – with the only exception that I have added a primary key property to both classes: public int Id { get; set; }. Otherwise EF would complain about a missing key property. This mapping doesn’t throw an exception and creates the following tables in the database:

    Parents table:

    - Id                    int             not nullable (PK)
    - Name                  nvarchar(MAX)   nullable
    - SelectedChild_Id      int             nullable (FK)
    - PublishedChild_Id     int             nullable (FK)
    

    Children table:

    - Id                    int             not nullable (PK)
    - Name                  nvarchar(MAX)   nullable
    - Father_Id             int             not nullable (FK)
    

    So, there are the three foreign key columns as expected.

    Since you get an exception according to your comment, I guess that there is actually some important difference in the code you have tested.

    BTW: Mapping the two navigation properties of the Parent class as One-to-One relationships is much more difficult, if not impossible. In EF you need a shared primary key between the two tables to map a One-to-One relationship, so it would not be possible to assign two different entities to the two navigation properties because they cannot both have the same key as the parent.

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

Sidebar

Related Questions

Lets say I have a simple many-to-many table between tables table1 and table2 that
I have a simple many-to-many relationship between the User class and the Place class.
I have a simple many to many relationship set up between tasks and tags.
I have a simple has_many through relationship set up: class Tag < ActiveRecord::Base has_many
I have a fairly simple Many to One relationship between two classes: @Entity public
I have a simple one-to-many relationship. I would like to select rows from the
I have a simple databasescheme: User, Account. User has 1-to-many relationship with Account. I
I'm trying to make simple many-to-one association, using NHibernate.. I have class Recruit with
I have a simple many to many relationship and I am wondering how you
Alright so I have a simple has_many :through relationship in Rails 3.1 class Event

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.