0

All,

I am working from the following MSDN Article Yet I am getting: "Cannot resolve symbol SavingChanges"

The call in question is:

contextProxy.SavingChanges += new EventHandler(context_SavingChanges);

contextProxy is of type: public class ReportingContext : DbContext which is my context class that handles all EF data models, etc. So if the issue is that DBContext does not inherit from whatever SavingChanges needs (ObjectContext?) how can I fix?

My implementation

public class ReportingProxy
{

    // Define the object context to be provided. 
    private ReportingContext contextProxy = new ReportingContext();

    public ReportingProxy()
    {
        // When the object is initialized, register the  
        // handler for the SavingChanges event.
        contextProxy.SavingChanges += new EventHandler(context_SavingChanges);
    }

    // Method that provides an object context. 
    public ReportingContext Context
    {
        get
        {
            return contextProxy;
        }
    }

    // SavingChanges event handler. 
    private void context_SavingChanges(object sender, EventArgs e)
    {
        var newClubIdToUpdate = 0;

        // Ensure that we are passed an ObjectContext
        ObjectContext context = sender as ObjectContext;
        if (context != null)
        {

            // Validate the state of each entity in the context before SaveChanges can succeed. 
            foreach (ObjectStateEntry entry in
                context.ObjectStateManager.GetObjectStateEntries(
                    EntityState.Added | EntityState.Modified | EntityState.Deleted))
            {

                if (SecurityHelper.IsValidNewClubTableName(entry.EntitySet.Name)) 
                {
                    if (!entry.IsRelationship && (entry.Entity.GetType() == typeof (NewClub)))
                    {
                        var nc = entry.Entity as NewClub;
                        newClubIdToUpdate = (nc != null ? nc.Id : 0);
                    }

                    if (!entry.IsRelationship && (entry.Entity.GetType() == typeof (NewClubProspect)))
                    {
                        var ncp = entry.Entity as NewClubProspect;
                        newClubIdToUpdate = (ncp != null ? ncp.NewClubId : 0);
                    }
                }
            }

            //Update the NewClub.LastActivityDate column
            if (newClubIdToUpdate > 0)
            {
                string q = @"UPDATE NewClub SET LastActivityDate='" + DateTime.Now + "' WHERE Id=" + newClubIdToUpdate;


                using (var cxt = new ReportingContext())
                {
                    var result = cxt.Database.ExecuteSqlCommand(q);
                }
            }
        }
    }
}
Tim
  • 14,999
  • 1
  • 45
  • 68
Slinky
  • 5,662
  • 14
  • 76
  • 130

1 Answers1

2

There's no SavingChanges event on DbContext. It is only on ObjectContext, which is what the MSDN article you linked to was about.

Here's an article on how you can get to it from the DbContext though.

To avoid link rot, here is the example modified for your scenario:

using (var contextProxy = new ReportingContext())
{
    var objCtx = ((IObjectContextAdapter)contextProxy ).ObjectContext;

    objCtx.SavingChanges += new EventHandler(context_SavingChanges);
}
Tim
  • 14,999
  • 1
  • 45
  • 68