Saving Linked Objects in SubSonic

I have 2 tables, Order and OrderItem, which have a 1-many relationship.

When I add a new order in the interface, how do I create a link. For instance.

(Order and OrderItem generated by SubSonic).
Order order = new Order();
//populate order details.


OrderItem item = new OrderItem();
//populate orderItem details.

      

How do I set up a relationship so that when storing them in the database they store the correct foreign key values, something like strings

item.setParent(order);

      

EDIT:

I tried to use

order.OrderItemRecords().Add(item);

      

but the error still occurs when updating the database.

0


a source to share


1 answer


(Order and OrderItem generated by SubSonic).
Order order = new Order();
//populate order details.


OrderItem item = new OrderItem();
//populate orderItem details.

item.Order = order;   //THIS LINE SETS THE PARENT OBJECT TO ABOVE ORDER

      

Remember to end the transaction and call save methods to commit this information to your database. You will add a reference to the System.Transactions namespace in your project and then a reference in your class.



eg

   using (TransactionScope scope = new TransactionScope())
    {
        try
        {
            Order order = new Order();
            //populate order details.
            order.Save(); //Commit to DB


            OrderItem item = new OrderItem();
            //populate orderItem details.

            item.Order = order;   //THIS LINE SETS THE PARENT OBJECT TO ABOVE ORDER

            item.Save();  //Commit to DB

            //complete you transaction
            scope.Complete();

        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            throw ex;
        }
    }

      

+3


a source







All Articles