调用SavingChanges时无法解决符号错误

本文关键字:解决 符号 错误 SavingChanges 调用 | 更新日期: 2023-09-27 18:04:09

全部,

我正在处理以下MSDN文章,但我得到了:"无法解决符号SavingChanges">

有问题的电话是:

contextProxy.SavingChanges += new EventHandler(context_SavingChanges);

contextProxy的类型为:公共类ReportingContext:DbContext,这是我的上下文类,用于处理所有EF数据模型等。因此,如果问题是DbContext没有继承SavingChanges所需的任何内容(ObjectContext?(,我该如何解决?

我的实现

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);
                }
            }
        }
    }
}

调用SavingChanges时无法解决符号错误

DbContext上没有SavingChanges事件。它只在ObjectContext上,这就是您链接到的MSDN文章的内容。

不过,这里有一篇关于如何从DbContext获得它的文章。

为了避免链接腐烂,下面是为您的场景修改的示例:

using (var contextProxy = new ReportingContext())
{
    var objCtx = ((IObjectContextAdapter)contextProxy ).ObjectContext;
    objCtx.SavingChanges += new EventHandler(context_SavingChanges);
}