Difference between Get and Load

前端 未结 4 837
失恋的感觉
失恋的感觉 2021-02-01 02:11

What\'s the difference between Get(object id) and Load(object id)? The documentation pretty much reads the same. Also, if it matter

4条回答
  •  别那么骄傲
    2021-02-01 03:05

    Load is the optimized way in some cases. Let's think of a Customer, Order relationship and assume that we have an Orders table with CustomerId as the foreign key.

    var order = new Order {OrderDate = Datetime.Now };
    order.Customer = session.Get(customerId);
    session.Save(order);
    

    Although we only need the customerId to persist the order object, above code block will first select the customer with that customerId from Customers table then hit the database again to insert the order for that customer.

    But if we used :

    order.Customer = session.Load(customerId);
    

    only the insert statement with that customerId will be executed. Load is the appropriate way in this case.

提交回复
热议问题