![](/img/trans.png)
[英]“Cannot create an instance of the abstract class or interface” C# error message
[英]Save Changes Error cannot call abstract class c#
这段代码在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
}
}
我的电话课如下
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)
{
}
}
}
类ContextBase
是抽象的,必须重写。 你已经做到了。 但是SaveChanges()
方法也是抽象的,也必须重写。
如果ContextBase
是您的发明,那么这些更改可能会有所帮助。
如果你想要求压倒一切,然后离开行:
public abstract bool SaveChanges();
...并完全删除基本方法。 然后只需在cmsContext
提供重写。 否则,将其删除并将基本方法更改为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);
}
}
对显然要使用的其他方法DeleteObject
。
使这些virtual
使您可以灵活地仅在派生类中需要与基类不同的特定功能时才进行覆盖。 否则,您只需使用您最初尝试的base.SaveChanges()
调用即可获得BaseContext
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.