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.