繁体   English   中英

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

[英]Cannot Resolve Symbol Error When Calling SavingChanges

所有,

我正在使用以下MSDN文章但我得到:“无法解析符号SavingChanges”

有问题的电话是:

contextProxy.SavingChanges += new EventHandler(context_SavingChanges);

contextProxy的类型是: public class 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 它只在ObjectContext ,这是你链接到的MSDN文章的内容。

这是一篇关于如何从DbContext获取它的DbContext

为避免链接损坏,以下是针对您的方案修改的示例:

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

    objCtx.SavingChanges += new EventHandler(context_SavingChanges);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM