win7user10 / Laraue.EfCoreTriggers

Library to write triggers in C# with EF.Core
MIT License
114 stars 22 forks source link

Multiple Condition In one trigger statement #46

Open sallamia opened 2 years ago

sallamia commented 2 years ago

How can I make multiple conditions for for example, that is to say (If ElseIf else)Example:

EntityTypeBuilder.AfterUpdate(trigger => trigger .Action(action => action .Condition((transactionBeforeUpdate, transactionAfterUpdate) => transactionAfterUpdate.Method== "cash") .Delete( (entityBeforeUpdate, entityAfterUpdate, transaction) => transaction.PaymentId == entityAfterUpdate.Id) .Condition((transactionBeforeUpdate, transactionAfterUpdate) => transactionAfterUpdate.Area== "bank") .Delete( (entityBeforeUpdate, entityAfterUpdate, cash) => cash.PaymentId == entityAfterUpdate.Id)) );

In this situation i get "And" not "Else if" How i can get in sql "If Payment.Method Else If Payment.Method............"

sharpmonkey commented 10 months ago

Similar thing tripped me up as well.

Trigger with two actions, each with own condition, however the generated code combines both conditions into one with AND operator followed by first Insert action statement (controlled by if) then immediately followed by the 2nd one (not controlled by if).

test

        [Fact]
        public void InsertTrigger_ShouldExecuteAction_OnlyWhenEitherConditionPasses()
        {
            Action<EntityTypeBuilder<SourceEntity>> builder = x =>
                x.AfterInsert(trigger => trigger
                    .Action(action => action
                        .Condition(tableRefs => tableRefs.New.IntValue < 10)
                        .Insert(inserted => new DestinationEntity()))
                    .Action(action => action
                        .Condition(tableRefs => tableRefs.New.IntValue > 20)
                        .Insert(inserted => new DestinationEntity()))
                    );

            using var dbContext = CreateDbContext(builder);

            dbContext.Save(new SourceEntity { IntValue = 12 });
            Assert.Empty(dbContext.DestinationEntities);

            dbContext.Save(new SourceEntity { IntValue = 9 });
            Assert.Single(dbContext.DestinationEntities);

            dbContext.Save(new SourceEntity { IntValue = 21 });
            Assert.Equal(2, dbContext.DestinationEntities.Count());
        }

Pseudo-code generated trigger:

IF(@NewIntValue < 10 AND @NewIntValue > 20)
insert into DestinationEntity
insert into DestinationEntity

expected trigger code:

IF(@NewIntValue < 10)
  insert into DestinationEntity
IF(@NewIntValue > 20)
  insert into DestinationEntity