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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:23:09+00:00 2026-05-22T14:23:09+00:00

Ok, I am total newbie so bear with me. Trying to implement an ordering

  • 0

Ok, I am total newbie so bear with me.
Trying to implement an ordering system and wish
to save the orders to the database with LINQ to Entities. I can do it now
but for each new object that is saved to the orders table
a new row is inserted, with new OrderNo for each ProductID where as I obviously
should be able to have multiple ProductID’s for each OrderNo.

Everything is very simplified as I am just testing.

I have an orders table with columns as such:

OrderNo PK, Identity specification
Line int PL
ProductID int

and a products table

ProductID int PK

An order entity object is instantiated and its properties
are populated with data from a form which is posted to an action method.
It is then saved to the orders table with the following code:

(DropDownList1Value) has value of an existing ProductID and “DropDownList1Value” is the id of the DropDownList element in view.

[HttpPost]
public ActionResult OrderProcessor()
{            
    int productId = int.Parse(Request.Form["DropDownList1Value"]);
    var order = new Order(); 
    order.ProductID = productId;
    context.Orders.AddObject(order);           
    context.SaveChanges();            
    return View(order);
}

So the records that are inserted look as such:
Sorry, couldn’t line up the values under their respective column name in this editor.

OrderNo Line ProductID

101     0    3
102     0    5
103     0    2

Where as I want something like this:

OrderNo  Line ProductID

101      1    3
101      2    5
101      3    2
102      1    2

So I wish to know how can I modify the orders table so it
can have multiple records with same “OrderNo” and just increment for “Line” for diff ProductID’s and how do I go about inserting such records with LinQ to Entities where
I will obviously have many ProductId from multiple DropDowLists
and they will all be for the one order.

Currently I have foreign key dependency on ProductID in Products table,
so no record in the Orders table can have ProductID which does not exists in the Products table.
I need to make the table depend on the whole key that is OrderNo + Line
and have the “Line” auto increment.

Or is there a much better way of implementing of what I am after here?

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-22T14:23:09+00:00Added an answer on May 22, 2026 at 2:23 pm

    Let me first explain briefly what I understood.

    There is an invoice, which contains several products for one order number.
    and this is how your invoice looks like:

    Order Number: 101
    ------------------
    Sl.       Products
    1         3
    2         5
    3         2
    

    Before answering I want to point out that you are taking OrderId from a form (That is from client side) This is a wrong and INSECURE approach. Let the order id be AutoGenerated by database.

    I would suggest to tweak your database design a little.
    Here is a solution that will work.
    Note: I am consedering your database support Auto-Increment, for MS SQL replace it with IDENTITY, for Oracle you need to create a sequence.

    Product (
         id        INT   PK  AUTO-INCREMENT
    );
    
    Order (
         id        INT   PK  AUTO-INCREMENT
         user-id   INT   FK                      # user who purchased
         ### and other common details Like date of purchase etc.
    );
    
    Order-Detail (
         id         INT   PK  AUTO-INCREMENT
         order-id   INT   FK                     # Common order id
         pdt-id     INT   FK                     # product which was purchased.
    );
    

    When you make a purchase:
    1. Insert a row in order table
    2. Fetch the last inserted id
    3. Insert order-id from last step and products which are purchased in Order-Detail table,

    Fetch all the orders made by a user:
    1. Read from order table.

    Fetch all products purchased for an order:
    1. Fetch details from Order-Detail
    Note: You will get List of products purchased, Use Order-detail.id as “Line”

    EDIT:
    Thanx to HLGEM’s comment
    If you think price of a product may change then instead of updating the price add a new row to the table (and flag the old table so that it wont be visible, you can also have a column in new table pointing to old table), thus old purchase will point to old product and new orders will point to updated (new) row.

    There is one more approach this problem:
    store the current cost of product in order-detail table.

    If you are facing difficulty understanding above solution here is another and simpler one.
    In Order table, Make a composite primary key including OrderNo and Line.
    Whenever inserting into database you will need to generate line number in your code, which you can do by runnign a loop over array of propduct being purchased.

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

Sidebar

Related Questions

Total newbie question but this is driving me mad! I'm trying this: myInt =
This is a total newbie question, so thanks in advance. I'm trying to get
I'm a total newbie when it comes to Javascript/jQuery so hopefully you can help
A total newbie question, so please bear with me here ;) When a key
I am trying to use boost string algorithms for case insensitive search. total newbie
total newbie here. i was trying to replace a character in char * but
Total newbie here, please bear with me. Building a very small personal app that
I'm a total newbie at Objective-C, so bear with me. This is how I'm
Total newbie at cronjobs and that kinda stuff, never done it before, so now
this is going to be a total newbie question but I can't really do

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.