CrudEvent with large body error + strange behavior

Two strange things happened today:

  1. I got an error when trying to update a model through AutoQuery:
    String or binary data would be truncated in table ‘StepsStage.dbo.CrudEvent’, column ‘RequestBody’.

It’s pretty obvious that the varchar(8000) limit was hit here. How can I make this larger, or accept truncation? Right now it crashes. (For now I’ve manually changed the data type in SQL Server)

  1. I have no idea why the CrudEvent was turned on for this data model. I’ve used it for a different datamodel, using the [AutoApply(Behavior.AuditCreate)] on the DTO, but not for this.

Here’s the DTO used for this query:

[ValidateHasPermission(Permissions.WriteFormDefinition)]
[Route("/formdefinitions/{Id}", Verbs = "PUT")]
public class UpdateFormDefinition : FormDefinition, IUpdateDb<FormDefinition>, IReturn<UpdateFormDefinitionResponse>
{
//...

FormDefinition is my type, and it is a POCO with no inheritance, defined like this:

[UniqueConstraint(nameof(Name), nameof(Version))]
public class FormDefinition
// ...

In my Configure.AutoQuery.cs I have not added anything “include all” or anything like that:

public class ConfigureAutoQuery : IConfigureAppHost, IConfigureServices
{
    public void Configure(IAppHost appHost)
    {
        appHost.Plugins.Add(new AutoQueryFeature {
            MaxLimit = 2000,
            IncludeTotal = true,           
        });

        // Create CrudEvent table if it doesn't exist
        appHost.Resolve<ICrudEvents>().InitSchema();    
    }

    public void Configure(IServiceCollection services)
    {
        services.AddSingleton<ICrudEvents>(c =>
                new OrmLiteCrudEvents(c.Resolve<IDbConnectionFactory>()));
    }
}

My Service class (since I’m overriding some):

[Authenticate]
[RequiredRole("Admin", ApplyTo = ApplyTo.Post | ApplyTo.Put)]
public class FormDefinitionServices : Service
{
    public FormRepository formRepo { get; set; }
    public IAutoQueryDb AutoQuery { get; set; }

    // AutoQuery/AutoCRUD doesn't have one for single (only query)
    public FormDefinition Get(SingleFormDefinition request)
    {
        return formRepo.GetFormDefinition(request.Id);
    }

    public FormDefinitionsResponse Get(SearchFormDefinitions request)
    {
       
    }

So the two questions are:

  1. Is it possible to configure CrudEvent to use a larger RequestBody column by default?
  2. Any idea why I got the CrudEvent/Audit stuff on the FormDefinition class?

The latest CrudEvent table should now use a Text or large string column when creating a new CrudEvent table.

If you had an existing CrudEvent table you’d need to change the modify the column definition manually.

If ICrudEvents is registered it gets run for every AutoQuery write table operation, the [AutoApply(Behavior.AuditCreate)] is to apply AutoQuery CRUD attributes to auto populate the AuditBase Created & Modified columns.

You’ll be able to ignore creating CrudEvents for specific Tables or APIs with something like:

appHost.GlobalMessageRequestFilters.Add((req, res, dto) => {
    if (dto is IUpdateDb<FormDefinition>)
        req.Items[Keywords.IgnoreEvent] = bool.TrueString;
});

Of course missing events would break the Executable Audit History property of CrudEvents.

1 Like

Thanks, I understand the explanation, and for that fix. Probably add the excellent info

If ICrudEvents is registered it gets run for every AutoQuery write table operation, the [AutoApply(Behavior.AuditCreate)] is to apply AutoQuery CRUD attributes to auto populate the AuditBase Created & Modified columns.

to the docs?

The docs already explain what AuditCreate does and the entire purpose of Crud Audit Events is to:

enable a recorded history of Executable Audit information over all AutoCrud operations

I’m sure it’s all there.
It’s not so easy with so much functionality sometimes, knowing when to use what, for what a layman sees at similar features (even though they obviously are different, they both contain the word “audit” for example).
My colleague says he’s overwhelmed (mostly in a positive way, but sometimes missing a TLDR).

1 Like