Skip to main content
.NET Framework 4.6.2+

XPBaseObject.Reload() Method

Reloads the state of the specified persistent object from the data store.

Namespace: DevExpress.Xpo

Assembly: DevExpress.Xpo.v24.2.dll

Declaration

public void Reload()

Remarks

Use the Reload method to refresh a persistent object’s state. The CannotLoadObjectsException is thrown if the specified persistent object is permanently deleted from the data store.

The Reload method does nothing in the following cases:

  1. It is called from a NestedUnitOfWork.
  2. The specified persistent object is a new object. To ensure that the persistent object is not a new object, use the Session.IsNewObject property.

Note

If an object’s state is reloaded within a transaction, the object is automatically removed from the list of objects that can be persisted via the Session.CommitTransaction method.

Example

The following example demonstrates how to work with transactions.

In this example, the transaction begins when a persistent object is saved. The Session.BeginTransaction method marks the starting point of the transaction. If the value of the persistent object’s Amount property is negative, the Session.RollbackTransaction method is called which completes the transaction discarding all the changes made since the transaction was started.

Otherwise, if the Amount property’s value isn’t negative, the Session.CommitTransaction method is called to complete the transaction and save all the data modifications made.

using System;
using DevExpress.Xpo;
using DevExpress.Xpo.DB;

namespace ConsoleApplication1 {
    class Account : XPObject {
        public const double DefaultAmount = 100;
        private double amount = DefaultAmount;
        public Account(Session session) : base(session) { }
        public double Amount {
            get { return amount; }
            set { SetPropertyValue<double>(nameof(Amount), ref amount, value); }
        }
        protected override void OnSaving() {
            base.OnSaving();
            if (!IsDeleted) {
                if (Amount < 0) {
                    throw new Exception("Negative amount!");
                }
            }
        }
    }

    class Program {
        static void Main(string[] args) {
            string connectionString = MSSqlConnectionProvider.GetConnectionString("(local)", "ConsoleApplication1");
            XpoDefault.DataLayer = XpoDefault.GetDataLayer(connectionString, AutoCreateOption.DatabaseAndSchema);
            TransferAmount(100);
            TransferAmount(200);
            Console.WriteLine("Press <Enter> to exit");
            Console.ReadLine();
        }

        static void TransferAmount(double amount) {
            using (Session session = new Session()) {
                Console.WriteLine("Creating Account1 with amount = " + Account.DefaultAmount);
                Account account1 = new Account(session);
                account1.Save();
                Console.WriteLine("Creating Account2 with amount = " + Account.DefaultAmount);
                Account account2 = new Account(session);
                account2.Save();
                Console.WriteLine("");
                Console.WriteLine("Beginning the transaction...");
                session.BeginTransaction();
                try {
                    Console.WriteLine(String.Format(" Withdrawing {0} from Account1", amount));
                    account1.Amount -= amount;
                    account1.Save();
                    Console.WriteLine(String.Format(" Transferring {0} to Account2", amount));
                    account2.Amount += amount;
                    account2.Save();
                    Console.WriteLine("Committing the transaction...");
                    session.CommitTransaction();
                }
                catch (Exception e) {
                    Console.WriteLine("Exception message: " + e.Message);
                    Console.WriteLine("Rolling back the transaction");
                    session.RollbackTransaction();
                    Console.WriteLine("Reloading the accounts from database...");
                    account1.Reload();
                    account2.Reload();
                }
                Console.WriteLine("Account1 amount: " + account1.Amount);
                Console.WriteLine("Account2 amount: " + account2.Amount);
                Console.WriteLine();
            }
        }
    }
}
See Also