Make NPoco Consider Database Defaults

Given the following table in my database (PostgreSQL)

 CREATE TABLE produkt ( id SERIAL NOT NULL, name VARCHAR(50), created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT timezone('utc'::text, now()) ); ALTER TABLE produkt ADD CONSTRAINT produkt_pkey PRIMARY KEY (id); 

I want to use NPoco to access data using this poco

 [TableName("produkt"), PrimaryKey("id", AutoIncrement = true)] public class Product { [Column("id")] public int Id { get; set; } [Column("name")] public string Name { get; set; } [Column("created_at")] public DateTime CreatedAt { get; set; } } 

Now when i call

 var product = new Product { Name = "My new Product" }; Database.Insert(product); // or Database.Save<Product>(product); 

created_at -column -infinity , even if default declared. Is there any way to fix this? One option would be to set Date in the application, but I would prefer that this functionality be implemented using the default value in the database.

+6
source share
1 answer

Try makin CreatedAt nullable:

[Column("created_at")] public Nullable<DateTime> CreatedAt { get; set; }

or set its value in the constructor, as shown here .

-2
source

Source: https://habr.com/ru/post/981276/


All Articles