4

I am converting a legacy application which uses SQL Server and I want to use petapoco for my data access layer.

The tables definitions contains a lot of columns with DEFAULT values.

I would like my DAL to handle the default value when I insert new records.

Unfortunatey, using the ResultColumn attribute is not a solution because the column is discarded when I use UPDATE and INSERT. A DEFAULT value does not act exactly as a readonly calculated field.

Is there a way to handle this ?

Larry
  • 17,605
  • 9
  • 77
  • 106

1 Answers1

7

There currently isn't a way because if the value is set to null then it will insert/update the value to DBNull.

There would have probably have to be a change to the code to have a new attribute eg. [OmitColumnIfDefaultValue] then if the value of that column equaled the default CLR value, then we could omit the column from the insert statement.

Schotime
  • 15,707
  • 10
  • 46
  • 75
  • Understood. In relation to http://stackoverflow.com/questions/230662/insert-default-value-when-parameter-is-null, some clear rules will have to be defined to make petapoco ignore a marked fill during insert : 1) the corresponding property must be decorated, 2) the value has to be set to null in the object before the insert. – Larry Jan 11 '12 at 08:40
  • When you say null, what happens if its an int...should you have to use a nullable int or if the value equals the default value in c#...eg. 0 for ints? – Schotime Jan 11 '12 at 09:27
  • For sure, the type has to be nullable. Considering DEFAULT is an option to avoid NULL values during INSERT for NOT NULL fields, this might fit well I think. – Larry Jan 11 '12 at 09:47