Entity Framework - delete an object in Oracle 10g {Operation is invalid due to the current state of the object}

I am trying to use a repository pattern in Entity Framework on Oracle 10g. I simplified my code:

Here is the SQL code:

-- 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 ); 

I am creating a simple repository class:

 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); } } 

The problem is that first I select some object, and then I want to delete. Choosing to work with Oracle DB is good. The problem is updating or deleting the command.

  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(); 

I get this exception:

{"An error occurred while updating records. Exception for details." }

Internal exception:

{"The operation is invalid due to the current state of the object." }

Stacktrace:

in System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand (DbModificationCommandTree commandTree) in System.Data.Mapping.Update.Internal.DynamicUpdateCommand.CreateCommand (UpdateTranslator translator, dictionary 2 identifierValues) at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary 2 identifierValues, List`1 generatedValues) in System.Data.Mapping.Update.Internal.UpdateTranslator.Update (IEntityStateManager stateManager, IEntityAdapter adapter)

I tested my repository class again. The MS SQL database works well.

What can cause this problem?

+4
source share
2 answers

I remember something like this:

 _context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified); 

linked post ObjectContext tracking changes in Entity Framwwork

For performance reasons, you can load an object without tracking changes.

+1
source

It looks like you are retrieving an object without using an ObjectContext.

Therefore, the loaded object is not context bound.

When you try to delete it through the context, the current state of the object is "disconnected", which is not valid for the delete operation.

0
source

Source: https://habr.com/ru/post/1379984/


All Articles