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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:37:37+00:00 2026-06-17T15:37:37+00:00

I have to Entities A and B . A has fields: id (PK) and

  • 0

I have to Entities A and B.

A has fields:
id (PK) and b_fk (FK to B's id).
B has similar fields:
id (PK) and a_fk (FK to A's id).

Now I want to create object A and B:

createAAndBMethod(Entities context){
    A a = new A();
    B b = new B();
    a.b_fk = b;
    b.a_fk = a;
    context.As.AddObject(a);
    context.Bs.AddObject(b);
}

someImportantMethod(){
    //do sth
    Entities context = new Entities(...);
    //do sth, maybe some changes to db on context
    createAAndBMethod(context);
    //do sth, maybe some changes to db on context
    context.SaveChanges();// <-- here I'm getting error
}

Saving doesn’t work with error: Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.

Is there any way to get this working with one save?
I need to create both objects in some code which shouldn’t be saving changes, so I can’t execute context.SaveChanges() anywhere before.

On context.SaveChanges() could happen something like:

 Create newA with nulled field b_fk
 Create newB with a_fk = newA
 Set newA.b_fk to newB

Update:
both a_fk and b_fk are nullable. I’m working with MS SQL and Azure SQL.

Update2:
I changed createAAndBMethod(Entities context)to:

createAAndBMethod(Entities context){
    A a = new A();
    context.As.AddObject(a);
    B b = new B();
    a.b_fk = b;
}

But it still doesn’t work with same error.

  • 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-06-17T15:37:38+00:00Added an answer on June 17, 2026 at 3:37 pm

    I think you model has a problem; A table has a foreign key reference to B table; and B table has a foreign key reference to A table. Then why don’t you merge these tables and only create a single table?

    If you are trying to create a one to one relationship, this is not the proper way.

    UPDATE:
    DB design is not proper. Only creating a foreign key in Table B is enough. there is no need to create a foreign key in Table A as such a design satisfies “A may have 0 or many Bs”.

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

Sidebar

Related Questions

I have a table Object that has 2 fields that are foreign keys (user_id
I have a model object which has entities annotated with hibernate validation annotations. For
I have two entities // All details has been removed @Entity @Table(name = "A_TABLE_NAME")
i have two entities one called User and another called Membership which has a
I have two entities: Patient and Job. Patient has a to-many relationship to Job
Lets say i have 2 Entities, User and Address. Each User has to have
In my Core Data model I have two entities: List and Patient. List has
Currently I have a database with the following relationships: One Client has multiple Entities
I have a problem with my site. The site has multiple entities: Articles, Posts,
I have a model that has an arbitrary number of children entities. For simplicity

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.