none
EF4 - OptimisticConcurrencyException - Insert table with IDENTITY column - no triggers, no misuse of settings RRS feed

  • Question

  • Hi
    I'm creating a 3-tier app, and I have a simple log table, as you can see below

    CREATE TABLE DBO.TBMOC002_LOG_EXEO (
      IDT_LOG       INT         IDENTITY,
      DAT_EXEO_PRSO    DATETIME       NOT NULL,
      NOM_PRSO_EXTD    VARCHAR(20)     NOT NULL,
      COD_RETO_APLI    TINYINT       NOT NULL,
      TXT_MENS_EXEO    VARCHAR(1024)    NOT NULL,
      CONSTRAINT PMOC002 PRIMARY KEY (IDT_LOG, DAT_EXEO_PRSO)
    )
    go

    This is my .edmx file definition

    SSDL content
        <EntityType Name="TBMOC002_LOG_EXEO">
         <Key>
          <PropertyRef Name="IDT_LOG" />
          <PropertyRef Name="DAT_EXEO_PRSO" />
         </Key>
         <Property Name="IDT_LOG" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
         <Property Name="DAT_EXEO_PRSO" Type="datetime" Nullable="false" />
         <Property Name="NOM_PRSO_EXTD" Type="varchar" Nullable="false" MaxLength="20" />
         <Property Name="COD_RETO_APLI" Type="tinyint" Nullable="false" />
         <Property Name="TXT_MENS_EXEO" Type="varchar" Nullable="false" MaxLength="1024" />
        </EntityType>
    And CSDL content
        <EntityType Name="TBMOC002_LOG_EXEO">
         <Key>
          <PropertyRef Name="IDT_LOG" />
          <PropertyRef Name="DAT_EXEO_PRSO" />
         </Key>
         <Property Name="IDT_LOG" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" ConcurrencyMode="None" />
         <Property Name="DAT_EXEO_PRSO" Type="DateTime" Nullable="false" ConcurrencyMode="None" />
         <Property Name="NOM_PRSO_EXTD" Type="String" Nullable="false" MaxLength="20" Unicode="false" FixedLength="false" />
         <Property Name="COD_RETO_APLI" Type="Byte" Nullable="false" />
         <Property Name="TXT_MENS_EXEO" Type="String" Nullable="false" MaxLength="1024" Unicode="false" FixedLength="false" />
        </EntityType>

    I have the method below that use a detached entity. I've tried both options, with AddObject and Attach + ChangeObjectState

    public void Incluir(TBMOC002_LOG_EXEO logExeo) 
    EntityConnection EConnectionMO = null; 
    try { 
    EConnectionMO =DConexao.EConexaoMO(); F5Context =new F5Entities(EConnectionMO); 
    //F5Context.TBMOC002_LOG_EXEO.AddObject(logExeo); 
     F5Context.ObjectStateManager.ChangeObjectState(logExeo, System.Data.
    EntityState.Added); F5Context.TBMOC002_LOG_EXEO.Attach(logExeo); 
    F5Context.SaveChanges();
    }
    catch (Exception Exc) 
    { 
    throw new Exception("Erro DlogExecucao.Incluir", Exc);
    F5Entities F5Context = null; 
    }

    I've receiving the error below:
    System.Data.OptimisticConcurrencyException was caught
      Message=Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.
      Source=System.Data.Entity
      StackTrace:
           at System.Data.Mapping.Update.Internal.UpdateTranslator.ValidateRowsAffected(Int64 rowsAffected, UpdateCommand source)
           at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
           at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
           at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
           at System.Data.Objects.ObjectContext.SaveChanges()
           at BBA.PrazoMedioAquisicao.Data.DLogExecucao.Incluir(TBMOC002_LOG_EXEO logExeo) in D:\F5\BBA.PrazoMedioAquisicao.Data\DLogExecucao.cs:line 34
      InnerException:

    I have other 5 tables with 5 methods and all works fine. But only this table have an IDENTITY column. No triggers, no Concurrency.

    Someone could help me ?

    • Edited by Larcolais Gong Wednesday, July 27, 2011 8:52 AM unreadable
    Thursday, July 21, 2011 11:58 AM

Answers

  • Finally I solve the problem by myself.... \o/

    I simply removed the field DAT_EXEO_PRSO from PK.

    And the code works fine !

    But I'm curious about this wrong behavior of EF4

    Thanks


    • Marked as answer by Fabio Venuto Friday, August 5, 2011 2:24 PM
    • Edited by Fabio Venuto Friday, August 5, 2011 5:49 PM comment
    Friday, August 5, 2011 2:24 PM

All replies