Laraue.EfCoreTriggers
Laraue.EfCoreTriggers copied to clipboard
Multiple Condition In one trigger statement
How can I make multiple conditions for <After Update> for example, that is to say (If ElseIf else)Example:
EntityTypeBuilder<Payment>.AfterUpdate(trigger => trigger .Action(action => action .Condition((transactionBeforeUpdate, transactionAfterUpdate) => transactionAfterUpdate.Method== "cash") .Delete<BankTransaction>( (entityBeforeUpdate, entityAfterUpdate, transaction) => transaction.PaymentId == entityAfterUpdate.Id) .Condition((transactionBeforeUpdate, transactionAfterUpdate) => transactionAfterUpdate.Area== "bank") .Delete<Cash>( (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............"
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