Dear Community,
I am using EntityEventListener to update a connected record of a records before deleting it using the following method:
public override void OnDeleting(object sender, EntityBeforeEventArgs e)
But when deleting the record it says there is connected records, I think this is linked to the count I am doing to check the number of records that match a condition.
Here the full code :
[EntityEventListener(SchemaName = "Activity")]
public class ActivityEventListener : BaseEntityEventListener
{
public override void OnDeleting(object sender, EntityBeforeEventArgs e)
{
base.OnDeleting(sender, e);
Entity activity = (Entity)sender;
var userConnection = activity.UserConnection;
var accountId = activity.GetTypedColumnValue<Guid>("AccountId");
var activityId = activity.GetTypedColumnValue<Guid>("Id");
int count = CountNumberOfActivityRdv(accountId, userConnection);
Guid ContactClientOuRepresentantNon = new Guid("f550b45d-093e-43ba-bdd1-bc0bd43c8e16");
if (count > 0)
{
var update = new Update(userConnection, "Account")
.Set("ContactClientOuRepresentantId", Column.Parameter(ContactClientOuRepresentantNon))
.Where ("Id").IsEqual(activityId.ToString());
update.Execute();
}
}
public int CountNumberOfActivityRdv(Guid accountId, UserConnection userconnection)
{
int count = 0;
var select = new Select(userconnection)
.Column(Func.Count("Id"))
.From("Activity")
.Where("ActivityCategoryId").IsEqual("42c74c49-58e6-df11-971b-001d60e938c6")
.And("AccountId").IsEqual(accountId.ToString()) as Select;
count = select.ExecuteScalar<int>();
return count;
}
Do I have to clear the cache or set the select to null ?