此代码在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)
{
}
}
}
答案 0 :(得分:0)
类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
中的内容。