OrmLight Guid not empty constraint - MySql

Is there a way to ensure that a Guid won’t be empty?

[Required]
public Guid Id { get; set;}

The required attribute allows empty guids to be persisted (I guess this can make sense in some scenario).

Thanks.

[Required] maps to a “NOT NULL” property in the RDBMS table. You can add a CheckConstraint for additional RDBMS validation (or trigger), otherwise you’d need to enforce the validation in your App.

My bad… I had already tried CheckConstraint but was missing the ’ ’ to enclose the empty guid:

Didn’t work:
[Required] [CheckConstraint(nameof(Id) + " != 00000000-0000-0000-0000-000000000000")]
public Guid Id { get; set;}

This works:

    [Required] [CheckConstraint(nameof(Id) + " != '00000000-0000-0000-0000-000000000000'")]
    public Guid Id { get; set;}

Thanks!

1 Like