none
Inserting a record to one to many relational tables using LINQ and C SHARP RRS feed

  • Question

  • Hi,

    I am using SQL Express 2008  and C sharp 2010. It is a windows form application.

    I have two tables such as Orders & Customers conneected togetter in one to many relation.

    my databse file is in a folder in drive C. (OUTSIDE THE APPLICATION FOLDER), In the future I will install this file in an organization server.

    I have the following code:

     Spider1Entities de = new Spider1Entities();
       
       Customer newCust = new Customer();
      
       newCust.Name = "GM";
    
       Order newOrder = new Order();
          
       newOrder.ProductName = "Car1";
    
       newCust.Orders.Add(newOrder);
       
       de.Customers.AddObject(newCust);
       
    
       de.SaveChanges();

     

    I would like to inseert a new order for an existing custome "GM", however, Each time when I run the application, the customer "GM" is inserted againg to the databse.

    so if I run the application 4 times, I insert "GM" to the customer table 4 times.

    Please let me know what is wrong in the code, and how I can add a new order to an existing customer.

     

    Thanks in aadvance for your support.

    Regards,

    JM


    Jmakmel
    Saturday, January 22, 2011 8:16 PM

Answers

  • Rather than creating a new Customer() and adding the newCust object each time, select the existing customer from the database and don't re-add it.

    Customer customer = from cust in de.Customers where cust.Name == "GM" select cust;

    if (customer == default (Customer))

    {

    // Add Customer if it doesn't exist

        customer = new Customer();

        customer.Name = "GM";

        de.Customers.AddObject(customer)

    }

       Order newOrder = new Order();
         
       newOrder.ProductName = "Car1";

       newCust.Orders.Add(newOrder);
       
       de.SaveChanges();

     

    Monday, January 24, 2011 11:19 AM

All replies

  • Rather than creating a new Customer() and adding the newCust object each time, select the existing customer from the database and don't re-add it.

    Customer customer = from cust in de.Customers where cust.Name == "GM" select cust;

    if (customer == default (Customer))

    {

    // Add Customer if it doesn't exist

        customer = new Customer();

        customer.Name = "GM";

        de.Customers.AddObject(customer)

    }

       Order newOrder = new Order();
         
       newOrder.ProductName = "Car1";

       newCust.Orders.Add(newOrder);
       
       de.SaveChanges();

     

    Monday, January 24, 2011 11:19 AM
  • Hello JM,

     

    Welcome to the MDSN Forum and thank you for posting here.

    I just found there are another three same threads posted by you in the MSDN Forum. I will delete them and keep this one open because it's not a good thing to be supported.

    Thank you for your understanding and support!

     

    Have a nica day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 24, 2011 1:45 PM
    Moderator