如何使用“一对多”在SubSonic中的关系 [英] How to use "one to many" relations in SubSonic

查看:208
本文介绍了如何使用“一对多”在SubSonic中的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 有什么方法可以告诉SubSonic这个关系(只有外键或者其他方法)?
  • 如果我有例如)一个团队对象与相关的团队成员

    **我如何从团队中访问和更新团队成员



    **我如何更新团队成员?保存团队对象是否能够保存团队成员的变更



    **如何向团队添加成员?我是否只创建一个新成员,将团队ID分配给外键并保存?或者是有更多的面向对象的方式(例如team.Add(teamMember))



解决方案

public Northwind.OrderDetailCollection OrderDetails()



这是一个BindingList,您可以根据需要更改,并调用SaveAll()来保存列表。没有深度保存,因此保存产品将不会保存相关的OrderDetail行。

  [Test] 
public void Demo_Product_OrderDetails()
{
Product product = new Product(3); //读取现有的行
OrderDetailCollection orderDetails = product.OrderDetails();
Assert.IsTrue(orderDetails.Count == 12);
foreach(OrderDetail orderDetail in orderDetails)
{
orderDetail.Discount - = 0; //做一些有意义的事情

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);

}


  • 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))

解决方案

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);

}

这篇关于如何使用“一对多”在SubSonic中的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆