Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Condition In one trigger statement #46

Open
sallamia opened this issue May 23, 2022 · 1 comment
Open

Multiple Condition In one trigger statement #46

sallamia opened this issue May 23, 2022 · 1 comment

Comments

@sallamia
Copy link

sallamia commented May 23, 2022

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants