【问题标题】:Entity Framework - Delete entity in Oracle 10g { Operation is not valid due to the current state of the object}实体框架 - 在 Oracle 10g 中删除实体 { 由于对象的当前状态,操作无效}
【发布时间】:2011-11-07 11:47:14
【问题描述】:

我尝试在 Oracle 10g 的实体框架中使用存储库模式。我简化了我的代码:

这里是 SQL 代码:

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

    }

 }

问题是首先我选择了一些实体,然后我想删除。 Select 适用于 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();

我得到了这个例外:

{"更新条目时发生错误。见内 详情例外。"}

内部异常:

{“由于对象的当前状态,操作无效。”}

堆栈跟踪:

在 System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree 命令树)在 System.Data.Mapping.Update.Internal.DynamicUpdateCommand.CreateCommand(UpdateTranslator 翻译,字典2 identifierValues) at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary2 identifierValues, List`1 generatedValues) at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager,IEntityAdapter 适配器)

我再次测试了我的存储库类 MS SQL 数据库,它运行良好。

什么会导致这个问题?

【问题讨论】:

    标签: entity-framework oracle10g


    【解决方案1】:

    我记得看到过这样的事情:

    _context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
    
    1. 加载/附加实体

    2. 将其标记为已更改/已跟踪

    3. 从上下文中删除/移除

    4. 保存

      MSDN 笔记http://msdn.microsoft.com/en-us/library/system.data.objects.objectstatemanager.aspx

    相关帖子ObjectContext tracking changes in Entity Framwwork

    出于性能原因,您可以在不进行更改跟踪的情况下加载对象

    【讨论】:

      【解决方案2】:

      看起来您在不使用 ObjectContext 的情况下检索对象。

      因此,加载的对象没有附加到上下文。

      当您尝试通过上下文删除它时,实体的当前状态为“分离”,这对于删除操作无效。

      【讨论】:

        猜你喜欢
        • 2012-02-02
        • 1970-01-01
        • 2010-10-18
        • 1970-01-01
        • 2018-03-18
        • 2020-04-30
        相关资源
        最近更新 更多