This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hello, When I used UnitOfWork.SaveChanges(), it saved all tables' changes to the database. Is it possible if I just choose to save one table's changes to the database? Thanks. |
|
|
No. The UnitOfWork is a scope, so calling SaveChanges() indicates you want to commit the work done in that scope. If you are familiar with the TransactionScope class and the way that is used, then think of the UnitOfWork in terms of a business transaction - you fetch objects, you may manipulate the state of those objects and then you may persist that state. If you want to scope your interaction to commit the changes to a single table then you will need to scope your UnitOfWork around those changes accordingly. Note that you can have multiple UnitOfWork instances active although each one will hold a database connection so you could scope things this way, or you could scope your UnitOfWork more tightly around the work you are doing. If you can elaborate on your scenario we could suggest an approach :)
Jeremy
|
|
|
Thanks for your reply. Could you please talk more details about 'scope your UnitOfWork around those changes accordingly' ? For example, I have a Customers table and Orders table. Different customers make orders, but I want to save orders for one of the customers to the database. How could I do that? Would you like to provide an example? |
|
|
I am assuming you have an existing customer and you are adding some new orders? If so, your code would look something like this in pseudo code.. using (var unitOfWork = [reference to LightSpeedContext object].CreateUnitOfWork()) { var customer = unitOfWork.Customers.SingleOrDefault(c => c.Id == existingCustomerId); customer.Orders.Add( new Order() { ProductCode = "MILK", Quantity = 2 } ); customer.Orders.Add( new Order() { ProductCode = "BREAD", Quantity = 1 } ); unitOfWork.SaveChanges(); // This will only save the newly added Order objects } // the UnitOfWork will be automatically disposed when you exit the using scope
This is very fine grained UnitOfWork scoping so you are very precisely controlling what is saved. You can scope in a number of ways (e.g. managed according to a thread or web request lifetime), so it depends on how your application is structured and how you are bounding your business transactions within the app.
Jeremy |
|