简体   繁体   中英

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

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. You've done that. But the method SaveChanges() is also abstract, and too must be overridden.

If ContextBase is your invention, then perhaps these changes would help.

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 . Otherwise, remove it and change the base method to 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 .

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. Otherwise, you simply use the base.SaveChanges() call you originally attempted and you get what is in BaseContext .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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