Ben Whitman
Ben Whitman

Reputation: 41

.ModifiedEntities is empty in SaveChanges_Executed() Lightswitch method

I'm new to Lightswitch and building an app that acts as a basic Change Management System.

I have an edit screen for these Change entities which successfully updates the data in the (SQL Server) database.

I want to call a stored proc in addition to the update after it's committed, to do some other stuff in the database and I'm trying to implement this with the following code:

partial void SaveChanges_Executed()
    {
        EntityChangeSet changes = this.DataWorkspace.ChangeControlData.Details.GetChanges();

        foreach (IEntityObject entity in changes.ModifiedEntities)
        {
            using (SqlConnection connection = new SqlConnection())
            {
                string connectionStringName = this.DataWorkspace.ChangeControlData.Details.Name;
                connection.ConnectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;

                string procedure = "dbo.usp_CompleteChange";
                using (SqlCommand command = new SqlCommand(procedure, connection))
                {
                    command.CommandType = CommandType.StoredProcedure;

                    command.Parameters.Add(new SqlParameter("@ChangeID", ((Change)entity).ChangeID));
                    command.Parameters.Add(new SqlParameter("@Loginname", ((Change)entity).StaffItem.Loginname));
                    command.Parameters.Add(new SqlParameter("@Supervisor", ((Change)entity).StaffItem.FullName));
                    command.Parameters.Add(new SqlParameter("@StatusName", ((Change)entity).Status.Name));

                    connection.Open();
                    command.ExecuteNonQuery();
                }
            }
        }
    }

However, by debugging I can see that the changes.ModifiedEntities collection is always empty whereas I would expect it to have one member, the Change record I am updating. So, the stored proc never gets called.

Any ideas?

thanks in advance!

Upvotes: 0

Views: 210

Answers (1)

Ozziemedes
Ozziemedes

Reputation: 311

Better late than never - I'm pretty sure it's because you've used the SaveChanges_Execut**ed** method, not the SaveChanges_Execut**ing** method. During the Executing method, nothing has been finalized, so the ModifiedEntities() collection still has "dirty" data to be written to the database. The Executed method can't access anything in ModifiedEntities() because the dirty writes have been flushed to the database after the Executing method completes.

If you want to persist an entity set between the two methods, you'd need to create a class-level attribute typed as an EntitySet, clone your ModifiedEntities() collection into it (casting the EntityChangeSet to an EntitySet) inside the Executing() method, then interrogate the cloned set in the Executed() method. Because an EntityChangeSet is an object, using an equality operator to assign it to your global variable would just set a reference to the object, and once the changes have been committed, the EntityChangeSet reference will disappear as well.

However, usual disclaimers apply around the use of global variables. You might be able to set it as a session variable and manage it's scope, but you'd still need to be wary about what might happen when a single user has multiple screens open concurrently, some of which might have their own changes to the same EntitySet. Concurrency management, complexity, yadda yadda yadda.

You're better off using the Executing() method to fire your stored proc.

Upvotes: 1

Related Questions