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

  • Home
  • SEARCH
  • 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 6748297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:32:54+00:00 2026-05-26T12:32:54+00:00

My entity classes are like this: public class A{ [Key] string Name; B DefaultB;

  • 0

My entity classes are like this:

public class A{  
  [Key]  
  string Name; 
  B DefaultB;  
  ICollection<B> Bs;  
}

public class B{  
  [Key]
  int Key;
}

public class C{  
  [Key]  
  string Key; 
  A MyA;
  B MyB;  

  public C(A a,B b){  
    MyA=a;  
    MyB=b;
  } 
}  

public class MyDbContext:DbContext{  
  public DbSet<A> As
  public DbSet<B> Bs
  public DbSet<C> Cs
}  

The testing code is like this:

main(){
  A a=new A();  
  B b=new B();  
  a.addB(B);  
  MyDbContext m=new MyDbContext();  
  m.As.Add(a);  
  m.SaveChanges();
  C c=new C(a,b);
  m.Cs.Add(c);
  m.SaveChanges();
}

The problem is when I add the c to the context, it tries to add a and b again in the context, which causes an exception because they are already in it. This should not happen.

How can I solve this?

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-26T12:32:55+00:00Added an answer on May 26, 2026 at 12:32 pm

    If you do this all in a single context you only need one single SaveChanges at the end:

    A a=new A();  
    B b=new B();  
    a.addB(B);
    
    using (MyDbContext m = new MyDbContext())
    {
       m.As.Add(a);  
    
       C c=new C(a,b);
       m.Cs.Add(c);
    
       m.SaveChanges();
    }
    

    EF will only create a single A and a single B and use both in the relationships in b and in c.

    If you need – for some reason – to create two contexts you must reattach the entites you inserted in the first context to avoid a second INSERT and duplication in the database:

    A a=new A();  
    B b=new B();  
    a.addB(B);
    
    using (MyDbContext m = new MyDbContext())
    {
       m.As.Add(a);  
       m.SaveChanges();
    }
    
    // ...
    
    using (MyDbContext m2 = new MyDbContext())
    {
       m2.As.Attach(a);
       m2.Bs.Attach(b);
    
       C c=new C(a,b);
       m2.Cs.Add(c);
    
       m2.SaveChanges();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have classes like this public class someList { public string strOne {get; set;}
I have an entity that looks like this: public partial class MemberTank { public
i have these 2 classes: public class Category { IDictionary<string, CategoryResorce> _resources; } public
there are two entity classes: @Entity public class Place implements Serializable { @Id @GeneratedValue(strategy
I have a Class like this: class ClassA { public long classAID {get; set;}
It seems like most examples of JPA/Hibernate entity bean classes I've seen do no
I have two entity classes annotated in the following way @Entity class A {
Say I have this interface: public interface IRepository<T> where T : Entity { T
We have the following classes @Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) // optional annotation as this
@Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) public class Problem { @ManyToOne private Person person; } @Entity

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.