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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:58:32+00:00 2026-06-02T17:58:32+00:00

I have one parent child entity : public class Transaction { public int TransactionId

  • 0

I have one parent child entity :

public class Transaction
{
  public int TransactionId {get; set;}
  public int? OrderId {get; set;}
  public virtual Order Order {get; set;}

} 

public class Order 
{
  public int Id {get; set;}
  public virtual List<OrderItems> OrderItems {get; set;}
}

public class OrderItem
{
  public int Id {get; set;}
  public string Item {get; set;}
  public virtual int OrderId {get; set;}
  public virtual Order Order {get; set;}
}

the context is as following :

   modelBuilder.Entity<Transaction>( )
            .HasKey( x => x.TransactionId )
            .ToTable( "Transactions" );

        modelBuilder.Entity<Transaction>( )
            .HasOptional( x => x.Order )
            .WithMany( )
            .HasForeignKey( t => t.OrderId );

        modelBuilder.Entity<Order>( )
            .HasKey( o => o.Id )
            .ToTable("Orders");

        modelBuilder.Entity<OrderItem>( )
            .HasKey( i => i.Id )
            .ToTable( "OrderItems" );


        modelBuilder.Entity<OrderItem>( )
            .HasRequired( x => x.Order )
            .WithMany( o => o.OrderItems )
            .HasForeignKey( p => p.OrderId );

When I try to create a new entity of type Transaction, something like :

var transaction = new Transaction 
                   {TransactionId = Guid.NewGuid;
                    Order = new Order {OrderItems = new List<OrderItems>{Item="SunCream"}}};

and

ctx.Transactions.Add(transaction);
ctx.SaveChanges();

I get

The INSERT statement conflicted with the FOREIGN KEY constraint
“FK_Transactions_Orders_OrderId”. The conflict occurred in database
“Transactions”, table “dbo.Orders”, column ‘Id’.

What exactly am I doing wrong here ?

  • 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-02T17:58:33+00:00Added an answer on June 2, 2026 at 5:58 pm

    I believe that you need to explicitly add the Order object to the ctx object first before you add the transaction. That way when you call the .SaveChanges() the context will know it has to commit the Order first before you commit the Transaction.

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

Sidebar

Related Questions

If I have entities set up for Parent - Child - Grandchild and one
I have a one-to-many mapping between a parent entity and child entities. Now I
Say I have the following POCO classes: public class Parent { public int ID
I have multiple level parent-child tables(GrandParent, Parent and Child, all many to one relation,
i have two Entities, Parent and Child, that are linked through a bidirectional one-to-many
I have a parent POM with several child modules. For one of these I
I have a main parent div that is .resizable(). One of it's child divs,
I have the following two models: class Parent < ActiveRecord::Base has_one :child, dependent: :destroy
I have one parent class, which is abstract class for now, for four different
I'm using Entity Framework 4 and have a one-to-many relationship between a parent and

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.