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.

nozzleman
  • 9,529
  • 4
  • 37
  • 58

1 Answers1

-2

Try makin CreatedAt nullable:

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

or set its value in constructor as seen here .

Community
  • 1
  • 1
IronBark
  • 74
  • 1
  • 8
  • 2
    Hey, thanks for your answer. I tried both `Nullable` 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! – nozzleman Jan 21 '15 at 09:35