Пример #1
0
        public virtual void SaveTest()
        {
            ModelInstance  userInstance = null;
            ModelSaveEvent saveEvent    = null;

            // Start a transaction to track identity changes that occur
            new ModelTransaction().Record(() =>
            {
                // Create a new user
                var user = CreateNewUser();

                // Get the model instance for the new user
                userInstance = ModelContext.Current.GetModelType(user).GetModelInstance(user);

                // Set the username for the new user
                user.UserName = "******";

                // Ensure that the model instance is new
                Assert.IsTrue(userInstance.IsNew, "Newly created instance was not marked as new.");

                // Save the new user instance
                saveEvent = Perform <ModelSaveEvent>(() => userInstance.Save()).FirstOrDefault();
            });

            // Ensure that the model instance has been saved correctly
            Assert.IsFalse(userInstance.IsNew, "New instance was not saved.");
            Assert.IsTrue(saveEvent != null && saveEvent.Instance == userInstance && saveEvent.Added.Count() == 1,
                          "The save event was not correctly raised during a save operation.");
        }