-- Create table create table TESTTABLE ( MODULE_UNIQUE_ID VARCHAR2(32) not null,PANEL_STATUS VARCHAR2(8) not null ) tablespace SYstem pctfree 10 pctused 40 initrans 1 maxtrans 255 storage ( initial 64K minextents 1 maxextents unlimited ); -- Create/Recreate primary,unique and foreign key constraints alter table TESTTABLE add constraint TESTTABLE_PK_01 unique (MODULE_UNIQUE_ID) using index tablespace SYstem pctfree 10 initrans 2 maxtrans 255 storage ( initial 64K minextents 1 maxextents unlimited );
我创建了简单的存储库类:
public interface IRepository<TEntity,TCtx> { TCtx Session { get;} void Add(TEntity entity); void AddOrAttach(TEntity entity); void Delete(TEntity entity); int Save(); TEntity SelectByKey(string colName,string key); } public class Repository<TEntity,TCtx> : IRepository<TEntity,TCtx>,Idisposable where TEntity : EntityObject where TCtx : ObjectContext { #region Private fields private TCtx _ctx; private string _keyProperty = "ID"; public int Save() { return _ctx.SaveChanges(); } public TEntity SelectByKey(string colName,string key) { KeyProperty = colName; // First we define the parameter that we are going to use the clause. var xParam = Expression.Parameter(typeof(TEntity),typeof(TEntity).Name); MemberExpression leftExpr = Expression.Property(xParam,KeyProperty); Expression rightExpr = Expression.Constant(key); BinaryExpression binaryExpr = Expression.Equal(leftExpr,rightExpr); //Create Lambda Expression for the selection Expression<Func<TEntity,bool>> lambdaExpr = Expression.Lambda<Func<TEntity,bool>> (binaryExpr,new ParameterExpression[] { xParam }); //Searching .... //IList<TEntity> resultCollection = ((IRepository<TEntity,TCtx>)this).SelectAll(new Specification<TEntity>(lambdaExpr)); //if (null != resultCollection && resultCollection.Count() > 0) //{ // //return valid single result // return resultCollection.First(); //} //return null; return ((IRepository<TEntity,TCtx>) this) .SelectAll(new Specification<TEntity>(lambdaExpr)).FirstOrDefault(); } public void Delete(TEntity entity) { _ctx.DeleteObject(entity); } }
问题是我先选择一些实体,然后我要删除.
选择适用于Oracle DB的工作.问题是更新或删除命令.
var _repo = new Repository<TESTTABLE,Entities>( new Entities(ConfigurationManager.ConnectionStrings["Entities"] .ConnectionString)); var obj = _repo.SelectByKey("MODULE_UNIQUE_ID","11111"); _repo.Delete(obj); _repo.Save();
我得到这个例外:
{“An error occurred while updating the entries. See the inner
exception for details.”}
内部异常:
{“Operation is not valid due to the current state of the object.”}
堆栈跟踪:
at
System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree
commandTree) at
System.Data.Mapping.Update.Internal.DynamicUpdateCommand.CreateCommand(UpdateTranslator
translator,Dictionary2 identifierValues) at
2
System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator
translator,EntityConnection connection,Dictionary
identifierValues,List`1 generatedValues) at
System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager
stateManager,IEntityAdapter adapter)
什么可能导致这个问题?
解决方法
_context.ObjectStateManager.ChangeObjectState(entity,EntityState.Modified);
>加载/附加实体
>将其标记为已更改/已跟踪
>从上下文删除/删除
>保存
MSDN Notes http://msdn.microsoft.com/en-us/library/system.data.objects.objectstatemanager.aspx
相关邮政ObjectContext tracking changes in Entity Framwwork
出于性能原因,您可以加载没有更改跟踪的对象
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。