0

I'm trying to use Petapoco in my Umbraco powered website. On my poco's I have a column called Created which has a default sql value (getDate()). I would like that column to be ignored on insert and update by Petapoco, but not on read.

Any idea how can I achieve that elegantly? perhaps with a custom mapper or including some new attributes in Petapoco engine (like IgnoreOnInsert, IgnoreOnUpdate).

I've been using some hacks, one of them was having two poco's for each table, one for insert-update and one for reading. But it's difficult to accept that as satisfactory.

Andrei
  • 31
  • 1
  • 6

2 Answers2

0

I usually just make a class constructor and set the date property there. Something like

public class WhatEvs
{
    ...
    public WhatEvs()
    {
        DateCreated = DateTime.Now;
    }
}
Jannik Anker
  • 3,320
  • 1
  • 13
  • 21
  • Yes, this was also one of the hacks, but I didn't like it 100% because then you need to always be careful to hydrate poco's before updating them, so you don't override the original value. That also means increasing the load on sql (and web server) as now before every save we need to make a read from sql. And besides that new developers in the team, if not instructed on this convention may do some code and override all the initial values for a poco. I would like a solution that does something similar to what attributes like IgnoreOnInsert, IgnoreOnUpdate would do. – Andrei Apr 08 '17 at 23:08
0

In case anyone is still interested in this, I eventually found a nice solution. And that is to stop using Petapoco and switch to NPoco, which has a nice attribute called 'ComputedColumn'. If you decorate a property with [ComputedColumn] attribute, that property will not be used when NPoco generates insert and update queries, but it will be used in select queries. That allows me to nicely have database handled values like CreatedDate or UniqueGuid which I don't touch, but am able to get when querying. Thanks to Aaron who kindly answered this problem here: https://github.com/CollaboratingPlatypus/PetaPoco/issues/421#issuecomment-348390149

Andrei
  • 31
  • 1
  • 6