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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:50:59+00:00 2026-06-10T21:50:59+00:00

I want to save a few entities with a bidirectional relationship (navigation properties on

  • 0

I want to save a few entities with a bidirectional relationship (navigation properties on both ends). This is accomplished by 2 calls to context.SaveChanges().

[The complete details about my model, mappings, and how I got there are after the fold.]

public void Save(){

     var t = new Transfer();
     var ti1 = new TransferItem();
     var ti2 = new TransferItem();

     //deal with the types with nullable FKs first
     t.TransferIncomeItem = ti1;
     t.TransferExpenseItem = ti2;

     context.Transfers.Add(t);
     context.Operations.Add(ti1);
     context.Operations.Add(ti2);

     //save, so all objects get assigned their Ids
     context.SaveChanges();

     //set up the "optional" half of the relationship
     ti1.Transfer = t;
     ti2.Transfer = t;
     context.SaveChanges();
} 

All’s well, but how about making sure the database isn’t inconsistent if lightning strikes beetween the two calls to SaveChanges() ?

Enter TransactionScope…

public void Save(){
    using (var tt = new TransactionScope())
    {
        [...same as above...]
        tt.Complete();
    }
}

… but this fails on the first call to context.SaveChanges() with this error:

The connection object can not be enlisted in transaction scope.

This question and this MSDN article suggest I explicitely enlist the transaction…

public void Save(){
    using (var tt = new TransactionScope())
    {
        context.Database.Connection.EnlistTransaction(Transaction.Current);

        [...same as above...]
        tt.Complete();
    }
}

…same error:

The connection object can not be enlisted in transaction scope.

Dead end here… Let’s go for a different approach – use an explicit transaction.

public void Save(){
    using (var transaction = context.Database.Connection.BeginTransaction())
    {
        try
        {
            [...same as above...]
            transaction.Commit();
        }
        catch
        {
            transaction.Rollback();
            throw;
        }
    }

Still no luck. This time, the error message is:

BeginTransaction requires an open and available Connection. The connection’s current state is Closed.

How do I fix this?


TL;DR details

Here’s my simplified model: a Transaction that references two operations (TransferItem) that reference back the transaction. This is a 1:1 mapping between Transfer and each of its two items.

What I want is to make sure these are saved atomically when adding a new Transfer.

Here’s the path I’ve walked, and where I got stuck.

The model:

public class Transfer
{
    public long Id { get; set; }
    public long TransferIncomeItemId { get; set; }
    public long TransferExpenseItemId { get; set; }
    public TransferItem TransferIncomeItem { get; set; }
    public TransferItem TransferExpenseItem { get; set; }
}

public class Operation {
    public long Id;
    public decimal Sum { get; set; }
}

public class TransferItem: Operation
{
    public long TransferId { get; set; }
    public Transfer Transfer { get; set; }
}

I want to save this mapping to the database (SQL CE).

public void Save(){
     var t = new Transfer();
     var ti1 = new TransferItem();
     var ti2 = new TransferItem();
     t.TransferIncomeItem = ti1;
     t.TransferExpenseItem = ti2;

     context.Transfers.Add(t);
     context.Operations.Add(ti1);
     context.Operations.Add(ti2);
     context.SaveChanges();
}

This blows in my face with the error:

“Unable to determine a valid ordering for dependent operations.
Dependencies may exist due to foreign key constraints, model
requirements, or store-generated values.”

Thsi is a chicken-and-egg problem. I can’t save objects with non-nullable foreign keys, but in order to populate the foreign keys, I need to save the objects first.

Looking at this question it seems that I have to relax my model, and:

  • have nullable FKs on at least one side of the relationship
  • save those objects first
  • set up the relationship
  • save again.

Like this:

public class TransferItem: Operation
{
    public Nullable<long> TransferId { get; set; }
    [etc]
}

Also, here are the mappings. Morteza Manavi’s article on EF 1:1 relationships was really helpful. Basically, I need to create one-many relationships with a specified FK column. The ‘CascadeOnDelete(false)’ deals with an error about multiple cascade paths. (The DB may try to delete Transfer twice, once for each relationship)

        modelBuilder.Entity<Transfer>()
            .HasRequired<TransferItem>(transfer => transfer.TransferIncomeItem)
            .WithMany()
            .HasForeignKey(x => x.TransferIncomeItemId)
            .WillCascadeOnDelete(false)
            ;

        modelBuilder.Entity<Transfer>()
            .HasRequired<TransferItem>(transfer => transfer.TransferExpenseItem)
            .WithMany()
            .HasForeignKey(x => x.TransferExpenseItemId)
            .WillCascadeOnDelete(false)
            ;

The updated code for saving the entities is at the beginning of the question.

  • 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-10T21:51:00+00:00Added an answer on June 10, 2026 at 9:51 pm

    To make this work, I had to add more fluent mappings, to explicitly create the optional mapping of the TransferItem on the Transfer class, as well as make the FK nullable on the TransferItem.

    I didn’t have a problem wrapping this all in a single TransactionScope, once the mappings were fixed.

    Here’s the entire console app:

      public class Transfer
       {
          public long Id { get; set; }
          public long TransferIncomeItemId { get; set; }
          public long TransferExpenseItemId { get; set; }
          public TransferItem TransferIncomeItem { get; set; }
          public TransferItem TransferExpenseItem { get; set; }
       }
    
       public class Operation
       {
          public long Id { get; set; }
          public decimal Sum { get; set; }
       }
    
       public class TransferItem : Operation
       {
          public long? TransferId { get; set; }
          public Transfer Transfer { get; set; }
       }
    
       public class Model : DbContext
       {
    
          public DbSet<Transfer> Transfers { get; set; }
          public DbSet<Operation> Operations { get; set; }
          public DbSet<TransferItem> TransferItems { get; set; }
    
          protected override void OnModelCreating( DbModelBuilder modelBuilder )
          {
             modelBuilder.Entity<Transfer>()
                .HasRequired( t => t.TransferIncomeItem )
                .WithMany()
                .HasForeignKey( x => x.TransferIncomeItemId )
                .WillCascadeOnDelete( false );
    
             modelBuilder.Entity<Transfer>()
                .HasRequired( t => t.TransferExpenseItem )
                 .WithMany()
                .HasForeignKey( x => x.TransferExpenseItemId )
                .WillCascadeOnDelete( false );
    
             modelBuilder.Entity<TransferItem>()
                .HasOptional( t => t.Transfer )
                .WithMany()
                .HasForeignKey( ti => ti.TransferId );
          }
       }
    
       class Program
       {
          static void Main( string[] args )
          {
    
             using( var scope = new TransactionScope() )
             {
                var context = new Model();
    
                var ti1 = new TransferItem();
                var ti2 = new TransferItem();
    
                //deal with the types with nullable FKs first
                context.Operations.Add( ti1 );
                context.Operations.Add( ti2 );
                var t = new Transfer();
                context.Transfers.Add( t );
                t.TransferIncomeItem = ti1;
                t.TransferExpenseItem = ti2;
                //save, so all objects get assigned their Ids
                context.SaveChanges();
    
                //set up the "optional" half of the relationship
                ti1.Transfer = t;
                ti2.Transfer = t;
                context.SaveChanges();
                scope.Complete();
             }
    
          }
       }
    

    When ran produced this database:

    enter image description here

    And this output:

    enter image description here

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

Sidebar

Related Questions

I am creating a few JAX-WS endpoints, for which I want to save the
I want to save a few lines as a file etl.xml I tried the
I have a class like this that I want to save in ViewState for
I want save the username and password when user login, and I added the
i want to save a variable to be global variable on all screens i
I want to save values as an ArrayList of double in a file. Whenever
I want to save webpage in document directory with the images,css and javascripts etc..
I want to save my program's DataModel objects to a file, and be able
I want to save a local copy of xml being ouput by a certain
I want to save a matrix to a text file, so I can read

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.