C# EF Core - simultaneous conflict with Trigger mariadb

Asked

Viewed 88 times

3

I’m using EF Core with mariadb, in the database I have fields with defined default values and a trigger "before insert".

In fluent mapping, I report the default values to the field normally. However, if I try to make the Insert in this table there is error of simultaneity, and I cannot find a native solution for this.

For testing, I removed the default values defined to the fields and also removed from the mapping, so it works. But I need these defined default values.

The error that is occurring is this:

"Database Operation expected to affect 1 Row(s) but Actually affected 0 Row(s). Data may have been modified or Deleted Since entities Were Loaded. See http://go.microsoft.com/fwlink/? Linkid=527962 for information on understanding and Handling optimistic concurrency exceptions."

Fluent mapping:

public class ProdutoConfig : IEntityTypeConfiguration<Produto>
{
    public void Configure(EntityTypeBuilder<Produto> builder)
    {
        builder.HasKey(p => new { p.Filial, p.ProdutoId });

        builder.Property(p => p.ProdutoId).HasColumnName("ProdutoId");
        builder.Property(p => p.Descricao).HasColumnName("Descricao");
        builder.Property(p => p.Valor).HasColumnName("Valor");
        builder.Property(p => p.Tipo).HasColumnName("Tipo").HasDefaultValue("F");
        builder.Property(p => p.Filial).HasColumnName("Filial");
    }
}

Trigger:

CREATE DEFINER=`root`@`localhost` TRIGGER `tg_produto_before_insert` BEFORE INSERT ON `produto` FOR EACH ROW BEGIN
    DECLARE idGerado INT;

    IF(NEW.ProdutoId IS NULL OR NEW.ProdutoId = 0) THEN

        SET idGerado = (SELECT COALESCE((SELECT UltimoGerado + 1 FROM Sequenciador WHERE Tabela = 'produto' AND Filial = NEW.Filial), 0));

        IF(idGerado = 0) THEN
            INSERT INTO Sequenciador (Filial, Tabela, UltimoGerado) VALUES (NEW.Filial, 'produto', 1);
            SET idGerado = 1;
        ELSE
            UPDATE Sequenciador SET UltimoGerado = idGerado WHERE Tabela = 'produto' AND Filial = NEW.Filial;
        END IF;

        SET NEW.ProdutoId = idGerado;

    ELSEIF(NEW.ProdutoId < 0) THEN
        SET NEW.ProdutoId = 
            (SELECT CASE WHEN COALESCE(ProdutoId, -1) > -1 THEN -1 ELSE (ProdutoId -1) END ProdutoId FROM Produto ORDER BY ProdutoId ASC LIMIT 1);
    END IF;
END
  • It makes available the code of the Fluent mapping. I believe that it can be something there.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.