![](/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.