views:

682

answers:

1
  • What are the ways to tell SubSonic about the relationship (only foreign keys? Or other methods too)?
  • If I have (for example) a team object with related team members

    ** how do I access and update the team members from the team

    ** how do I update the team members? Does saving the team object saves the team members changes

    ** How do I add members to the team? Do I just create a new member, assign the team ID to the foreign key and save? Or is there a more object oriented way (e.g. team.Add(teamMember))

+3  A: 

Subsonic code generation will read the foreign key relationships in the tables and create the required helper methods in the table classes. The Northwind Product class has a PrimaryKey relationship to the OrderDetail class. Subsonic generates the method

public Northwind.OrderDetailCollection OrderDetails()

to get the OrderDetail rows as an OrderDetailCollection. This is a BindingList that you can change as needed, and call SaveAll() to save the list. There is no deep saving, so saving the Product won't save related OrderDetail rows.

[Test]
public void Demo_Product_OrderDetails()
{
    Product product = new Product(3); // Read an existing row.
    OrderDetailCollection orderDetails = product.OrderDetails();
    Assert.IsTrue(orderDetails.Count == 12);
    foreach(OrderDetail orderDetail in orderDetails)
    {
        orderDetail.Discount -= 0; // Do something meaningful.
    }
    OrderDetail newDetail = new OrderDetail();
    newDetail.ProductID = 3;
    newDetail.OrderID = 10248;
    newDetail.UnitPrice = 7.00m;
    newDetail.Discount = 0.10f;
    newDetail.Quantity = 12;
    orderDetails.Add(newDetail);
    orderDetails.SaveAll();

    orderDetails = product.OrderDetails();
    Assert.IsTrue(orderDetails.Count == 13);

    OrderDetail.Destroy(newDetail.OrderID);

    orderDetails = product.OrderDetails();
    Assert.IsTrue(orderDetails.Count == 12);

}
P a u l
Is foreign key relationships the only way to do this in SubSonic? Any naming conventions?
tyndall