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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:49:25+00:00 2026-05-28T19:49:25+00:00

I have a database similar to below: Order =============== OrderID Description EmployeeID …other fields

  • 0

I have a database similar to below:

Order
===============
OrderID
Description
EmployeeID
...other fields


Product
===============
ProductID
...other fields


OrderProducts
===============
OrderID
ProductID


Employee
===============
EmployeeID
...other fields

I’m using Linq to Entities and the edmx file has been created without the OrderProducts table, as it is just a joining table. The Products table is a list of static products – I dont need to insert any rows at the moment. The Order table I can insert rows successfully using the following code:

[Serializable]
public class MyOrderObject
{
   public int OrderID { get; set; }
   public string OrderDescription { get; set; }
   public int? EmployeeID { get; set; }
   public IEnumerable<MyProductObject> ProductsList { get; set; }
   ...other fields
}

[Serializable]
public class MyProductObject
{
   public int ProductID { get; set; }
   ...other fields
}


private static void AddNewOrder(MyOrderObject order)
{
   using (var context = DatabaseHelper.CreateContext())
   {
      var dbOrder = new Order
      {
         OrderID = order.OrderID,
         Description = order.OrderDescription,
         Employee = context.Employees.SingleOrDefault(x => x.EmployeeID == order.EmployeeID),
      }
      context.AddToOrders(dbOrder);
      context.SaveChanges();
   }
}

How do I insert into the database my list of child relationship records??
I’ve tried:

List<int> ProductIDs = order.ProductsList.Select(x => x.ProductID).ToList();
//dbOrder.Products.Attach(context.Products.Where(x => ProductIDs.Contains(x.ProductID)));
//or dbOrder.Products = context.Products.Where(x => ProductIDs.Contains(x.ProductID));
//or dbOrder.Products = context.Products.Contains(ProductIDs);
//or foreach(var p in order.ProductsList)
//   {
//      context.AttachTo("Products", new Product { ProductID = p.ProductID });
//   }
  • 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-28T19:49:26+00:00Added an answer on May 28, 2026 at 7:49 pm

    No need to change the object type:

    var dbOrder = new Order
    {
       OrderID = order.OrderID,
       Description = order.OrderDescription,
       Employee = context.Employees.SingleOrDefault(x => x.EmployeeID == order.EmployeeID),
    }
    
    //this adds the relationship to the child without adding a new child record - perfect!
    foreach(var p in order.ProductsList)
    {
       dbOrder.Products.Add(p);
    }
    
    context.AddToOrders(dbOrder);
    context.SaveChanges();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text similar to the below in my database this is my
I have quite a few situations where I have database structures similar to: TABLE
I have a database schema that is similar to the following: | User |
Where I have more than one table in my database for use with (similar,
I currently have a database like the picture below. Where there is a query
I have a database which stores employees leave. For each day an employee is
I have a database results table with the following fields: game_id, home_team, away_team, home_team_score,
I have a MySQL database that looks similar to this: ID Group Name 1
I have to load the data shown in the below image into my database.
I have 4 databases with similar schema's, and I'm trying to create a query

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.