简体   繁体   English

保存更改错误无法调用抽象类C#

[英]Save Changes Error cannot call abstract class c#

This code worked fine in ef 4.0 but I have just upgraded to ef 5.0 and got the following error on calling save changes 这段代码在ef 4.0中工作正常,但是我刚刚升级到ef 5.0,并在调用保存更改时遇到以下错误

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using OSPos.DBContext;

namespace OSPos.OSPosContext
{
    public abstract class ContextBase
    {
        public abstract bool SaveChanges();

        protected bool SaveChanges(ObjectContext context)
        {
            try
            {
                int i = context.SaveChanges();
                if (i == 0)
                    return false;

                return true;
            }
            catch (Exception ex)
            {
                throw new EntityContextException("SaveChanges failed.", ex);
            }

        }

        public abstract void DeleteObject(object entity);

        protected void DeleteObject(ObjectContext context, object entity)
        {
            try
            {
                context.DeleteObject(entity);
            }
            catch (Exception ex)
            {
                throw new EntityContextException("DeleteObject failed.", ex);
            }
        }

        protected static string pamsConnectionString;

        public static string PamsConnectionString
        {
            set { pamsConnectionString = value; }
        }

        protected static string pamssysdbfConnectionString;

        public static string PamssysdbfConnectionString
        {
            set { pamssysdbfConnectionString = value; }
        }

        #region Encryption Fields

        public static string encryptionServiceUrl;
        public static bool encryptionEnabled = false;
        private static string encryptionTicket;

        #endregion
    }
}

My calling class is as follows 我的电话课如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.ComponentModel;
using System.Text.RegularExpressions;
using CMSNIDataobjects.Lookups;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using OSPos.OSPosContext;

namespace OSPos.OSPosContext
{
    public class cmsContext : ContextBase
    {
        private  OSPosEntities1  _OsPosEntities;
        protected OSPosEntities1 OSPosEntities
        {
             get
             {
                 if (_OsPosEntities == null)
                 {
                 // try
                 // {
                        _OsPosEntities = new OSPosEntities1();//knowledgebaseEntities1(@"metadata=res://*/KnowledgeBase.csdl|res://*/KnowledgeBase.ssdl|res://*/KnowledgeBase.msl;provider=System.Data.SqlClient;provider connection string="data source=DAVID_BUCKLEY\DBSQLSERVER28R2;initial catalog=knowledgebase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"");}
                 //}
                 //catch (Exception ex)
                 //{
                 //    throw new EntityContextException("cmsEntities object could not be created.", ex);
                 //}
                }
                return _OsPosEntities;
            }
        }

        public override int  SaveChanges()
        {
            return base.SaveChanges();
            //this is where i am getting the error in reference to abstract class
        }

        public override void DeleteObject(object entity)
        {
        }
    }
}

The class ContextBase is abstract and must be overridden. ContextBase是抽象的,必须重写。 You've done that. 你已经做到了。 But the method SaveChanges() is also abstract, and too must be overridden. 但是SaveChanges()方法也是抽象的,也必须重写。

If ContextBase is your invention, then perhaps these changes would help. 如果ContextBase是您的发明,那么这些更改可能会有所帮助。

If you want to require overriding, then leave the line: 如果你想要求压倒一切,然后离开行:

public abstract bool SaveChanges();

... and remove the base method altogether. ...并完全删除基本方法。 Then simply provide the override in cmsContext . 然后只需在cmsContext提供重写。 Otherwise, remove it and change the base method to virtual : 否则,将其删除并将基本方法更改为virtual

protected virtual bool SaveChanges(ObjectContext context)
{
    try
    {
        int i = context.SaveChanges();
        if (i == 0)
            return false;

        return true;
    }
    catch (Exception ex)
    {
        throw new EntityContextException("SaveChanges failed.", ex);
    }
}

Do the same for the other method you obviously want to use, DeleteObject . 对显然要使用的其他方法DeleteObject

Making these virtual will allow you the flexibility to override only when you need specific functionality in the derived class that differs from the base class. 使这些virtual使您可以灵活地仅在派生类中需要与基类不同的特定功能时才进行覆盖。 Otherwise, you simply use the base.SaveChanges() call you originally attempted and you get what is in BaseContext . 否则,您只需使用您最初尝试的base.SaveChanges()调用即可获得BaseContext

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

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