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