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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:47:57+00:00 2026-05-31T16:47:57+00:00

I have the following C# class: public class SomeClass{ public string Name; public List<SomeClass>

  • 0

I have the following C# class:

public class SomeClass{
   public string Name;
   public List<SomeClass> List1;
   public List<SomeClass> List2;
}

When I have 2 instances of SomeClass (SomeClass1 and SomeClass2) and do something like:

SomeClass1.List1.Add(SomeClass2);

It also adds SomeClass1 to SomeClass2.List2 for some reason when context.SaveChanges() is called. How can I prevent this?

Edit: I am using code first.
Thanks

  • 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-31T16:47:58+00:00Added an answer on May 31, 2026 at 4:47 pm

    If you don’t have any mapping with Fluent API EF will create a mapping based on conventions. The convention in this case is that List1 and List2 are the inverse navigation properties of the same relationship – in this case a “self-referencing” many-to-many relationship.

    The effect you experience is an automatic relationship fixup which is executed when DetectChanges() is called (or SaveChanges() which calls DetectChanges() internally). This fixup updates inverse navigation properties of attached entities automatically so that they are consistent with each other. You cannot prevent this and it shouldn’t actually be a problem.

    Edit

    If you don’t want a mapping between the two lists you must specify an explicit mapping with Fluent API. For example: If both lists belong to separate one-to-many relationships you must specify:

    modelBuilder.Entity<SomeClass>()
        .HasMany(s => s.List1)
        .WithRequired()              // or WithOptional()
        .WillCascadeOnDelete(false);
    
    modelBuilder.Entity<SomeClass>()
        .HasMany(s => s.List2)
        .WithRequired()              // or WithOptional()
        .WillCascadeOnDelete(false);
    

    This mapping says that the second end of each relationship is not exposed in the model (hence WithRequired()/WithOptional() without parameter).

    Similar you can specify two many-to-many relationships with HasMany(s => s.List1/2).WithMany().Map(...).

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

Sidebar

Related Questions

Let's say I have the following class definition : List<SomeClass>... public class SomeClass {
I had, and have since corrected, something like the following code: class SomeClass {
I have the following class public class Car { public Name {get; set;} }
I have the following class: public abstract class AbstractParent { static String method() {
I have the following class public class UIControl { public string FName{ get; set;
I have the following class public class CountrySpecificPIIEntity { public string Country { get;
I have the following class public class CountrySpecificPIIEntity { public string Country { get;
I have the following class: public class LooklessControl : Control { public List<int> IntList
I have a class like this class someclass{ public function somemethod(){} } Now I
I have the following Interview question: class someClass { int sum=0; public void foo()

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.