Make NPoco consider Database default values
Asked Answered
F

1

7

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 the 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);

The created_at-column is -infinity, even though the default value has been declared. Is there any way to fix this? One alternative would be to set the Date within the application, but I would rather have this functionality beeing realized using the default value in the DB.

Fete answered 20/1, 2015 at 13:8 Comment(0)
M
-2

Try makin CreatedAt nullable:

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

or set its value in constructor as seen here .

Morphosis answered 21/1, 2015 at 8:53 Comment(1)
Hey, thanks for your answer. I tried both Nullable<DateTime> as well as DateTime?, but neither did the trick. Maybe it is a Postgre Issue. Setting the Date in the CTor would be an Alternative I know, but I would prefer setting it in the DB to stay consistent. Anyway, thank you very much!Fete

© 2022 - 2024 — McMap. All rights reserved.