0

this my table structure. As you see here I defined the operation's number as an auto-generated value

and this the mapping of this table: numOperation is defined autoGenerated increment and so on

modelBuilder.Entity<OperationComptable>(entity =>
            {
                entity.HasKey(e => e.NumOp)
                    .HasName("PK_OperationComptable");

                entity.Property(e => e.NumOp).ValueGeneratedOnAdd();

                entity.Property(e => e.DateOp).HasColumnType("date");

                entity.Property(e => e.LibelleOp)
                    .IsRequired()
                    .HasMaxLength(300);

                entity.Property(e => e.NatureOp).HasMaxLength(100);

                entity.Property(e => e.NumPieceJustificatifOp)
                    .IsRequired()
                    .HasMaxLength(50);

                entity.HasOne(d => d.IdExerciceComptableNavigation)
                    .WithMany(p => p.OperationComptable)
                    .HasForeignKey(d => d.IdExerciceComptable)
                    .OnDelete(DeleteBehavior.Restrict)
                    .HasConstraintName("FK_OperationComptable_OperationComptable");

                entity.HasOne(d => d.NumOpNavigation)
                    .WithOne(p => p.OperationComptable)
                    .HasForeignKey<OperationComptable>(d => d.NumOp)
                    .OnDelete(DeleteBehavior.Restrict)
                    .HasConstraintName("FK_OperationComptable_User");
            });

when I create new entity,i got this error "System.Data.SqlClient.SqlException: Impossible d'insérer une valeur explicite dans la colonne identité de la table 'OperationComptable' quand IDENTITY_INSERT est défini à OFF" It seems weird because i didn't set the identity when i made my new instance. This process will be performed by a generic repository, more specifically this method :

public virtual TEntity Add(TEntity entity)
        {
            TEntity _entity = _unitOfWork.set<TEntity>().Add(entity).Entity;
            _entities.Entry(entity).State = EntityState.Added;
            return _entity;
        }

which will be called by a generic service exactly this sample of code:

public virtual TModel Create(TModel model)
        {           
            _logger.LogDebug("{method} called", nameof(Create));
            if (model == null)
            {
                throw new ArgumentNullException("entity " + typeof(TModel));
            }

            try
            {
                TEntity _entity = _builder.BuildModel(model);
                _entity = _repository.Add(_entity);
                _unitOfWork.Commit();
                return _builder.BuildEntity(_entity);
            }
            catch(Exception e)
            {
                return null;
            }           
        }
Ahmed Chebbi
  • 9
  • 1
  • 1
  • 8
  • Translation: Can not insert an explicit value in the Identity column of the 'OperationComptable' table when IDENTITY_INSERT is set to OFF. – swatsonpicken May 23 '17 at 10:04
  • You should change the title – Thomas Ayoub May 23 '17 at 10:04
  • I'm pretty sure a similar question has been asked before and has already been answered. – Filburt May 23 '17 at 10:09
  • 1
    I'd try what's suggested on this one: https://stackoverflow.com/questions/11173562/entity-framework-error-cannot-insert-explicit-value-for-identity-column-in-tabl – Zalomon May 23 '17 at 10:15
  • I solved this problem. An error happened somehow while the table creation in sql server. When I deleted and re-created the primary key the table everything goes well .. Thank you anyway – Ahmed Chebbi May 23 '17 at 21:01

0 Answers0