这是我的表结构。如您所见,我将操作号码定义为自动生成的值
这个表的映射: 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");
});
当我创建新实体时,我收到此错误“System.Data.SqlClient.SqlException:Impossible d'insérerunevaleur explicite dans lacolonneidentitédela table'OperationComptable'quand IDENTITY_INSERTestdéfiniàOFF” 这看起来很奇怪,因为当我创建新实例时,我没有设置身份。 此过程将由通用存储库执行,更具体地说,此方法:
public virtual TEntity Add(TEntity entity)
{
TEntity _entity = _unitOfWork.set<TEntity>().Add(entity).Entity;
_entities.Entry(entity).State = EntityState.Added;
return _entity;
}
将由通用服务完全调用此代码示例:
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;
}
}