OrmLite [Required] attribute inconsistency

Hi

Latest SS, C# nullable enabled, PostgreSQL, not tried on other DB servers.
I have interface which I want to enforce both on DTO and DB classes:

public interface IDemo
{
  string? StringData { get; set; }
  DateTimeOffset? DateTimeOffsetData { get; set; }
  long? LongData { get; set; }
}

DTO:

public class PostDemo: IDemo, IPost, IReturnVoid
{
  public string? StringData { get; set; }
  public DateTimeOffset? DateTimeOffsetData { get; set; }
  public long? LongData { get; set; }
}

DB:

[Alias("demos")]
public class DemoDb: IDemo
{
  [AutoIncrement]
  public long? Id { get; set; }

  [Required]
  public string? StringData { get; set; }

  [Required]
  public DateTimeOffset? DateTimeOffsetData { get; set; }

  [Required]
  public long? LongData { get; set; }
}

Generated table DDL is generating non consistent “not null” regarding to attribute.

create table public.demos (
  id bigint primary key not null default nextval('demos_id_seq'::regclass),
  string_data text not null,
  date_time_offset_data timestamp with time zone,
  long_data  bigint
)

All fields are defined as nullabile in interface and on applied classes.
While this is perfectly fine for DTOs, I’m having issue with generated table in database itself where [Required] attribute is not applied consistently.
On string fields it is applied and StringData is declared as “not null”, however other types are not-“not null”.

I know that i can set long LongData { get; set; } instead of long? LongData { get; set; } but I’m getting warning that I’m not conformant with interface because of nullability.

Is there a way to to declare fields in DB class as nullable and enforce [Required] attribute so that fields are “not null” in database?

Thanks.

The issue here is that you’re using nullable value types which are different types than its value types, i.e. int? is an alias for Nulllable<Int32> which isn’t the same type as Int32. Whereas nullable reference types like string? is the same runtime type as string just with an annotation for the compiler.

OrmLite only used to render null columns for nullable value types, but I’ve updated it so that nullable value types with [Required] are treated as non-nullable Value Types.

This change is available from the latest v6.11.1+ that’s now available in the Pre Release packages.

1 Like

Thank you for swift response.
Great support as always.

1 Like