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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:50:30+00:00 2026-05-27T00:50:30+00:00

I have a EF Code First One-To-Many relationship. It’s essentially a Parent/Child relations as

  • 0

I have a EF Code First One-To-Many relationship. It’s essentially a Parent/Child relations as the child can’t exist without the parent.

public class Parent
{
    [Key]
    public Guid Id { get; set; }

    public virtual ICollection<Child> Children { get; set; }

    public virtual ICollection<OtherChild> OtherChildren { get; set; }
}

public class Child
{
    [Key]
    public Guid Id { get; set; }

    public virtual Parent Parent { get; set; }
}

So I wasn’t sure how I could have the child be required to have a Parent so I tried putting a [Required] attribute on it. That gave me the error:

- InnerException {"Introducing FOREIGN KEY constraint 'Child_Parent' on table 'Child'
  may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE
  NO ACTION, or modify other FOREIGN KEY constraints.\r\nCould not create constraint.
  See previous errors."}
  System.Exception {System.Data.SqlClient.SqlException}

Ok I’m not sure how he could have multiple cascading parts.

The parent also has other child objects and those child objects share a many-to-many relationship with the original child object but it shouldn’t require a cascade delete.

I guess I’m doing this wrong but what is the proper way to do this.

PS. When I have a child require a parent should I make the foreign key a part of the primary key?

  • 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-27T00:50:31+00:00Added an answer on May 27, 2026 at 12:50 am

    You can disable cascading delete for the relationship in Fluent API (it’s not possible with data annotations):

    public class MyContext : DbContext
    {
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Parent>()
                .HasMany(p => p.Children)
                .WithRequired(c => c.Parent)
                .WillCascadeOnDelete(false);
        }
    }
    

    You must delete the children then as well in your code if you delete a parent.

    You don’t need to make the foreign key part of the primary key and I don’t see a benefit in doing so. Your Guid key is already unique. It will help query performance though if you create an index on the foreign key column in the database.

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

Sidebar

Related Questions

I have some simple Code First classes with a Many to One relationship. If
I'm struggling with creating a simple one-to-many relationship using Code First in EF. I
Is it possible to create a One-to-Many relationship with Code First that uses a
Racking my brains on this one. I have the code below: the first stages
I have the following code: Public Shared Function GetAvailableManufacturers() As List(Of Manufacturer) 'first get
I have a one-to-many relationship between Part and Params (a Part has many Params).
I seem to have major problems getting a one to many relationship to work
I have a recursive one-to-many relationship that has the default lazy value of true.
When performing a delete of a one-many relationship without exposing the foreign key, EF
I'm trying to make a one to many self reference using EF4.1 code first.

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.