Rafael Costa
Rafael Costa

Reputation: 87

How can I force a table column type using Petapoco?

I desire force a datetime mysql column type, because petapoco is creating a timestamp column.

[TableName("test"), PrimaryKey("id")]
public class Test
{
        [Column("id")]
        public long id { get; set; }

        [Column("datetime")]
        public DateTime datetime { get; set; }
}

Column decorator has a second parameter "Named Parameters". How can I use it?

Upvotes: 0

Views: 1622

Answers (2)

Eduardo Molteni
Eduardo Molteni

Reputation: 39413

You can mark the column as ResultColumn, that way the column is retrieved on selects, but isn't included in UPDATE and INSERT

    [ResultColumn("datetime")]
    public DateTime datetime { get; set; }

or just

    [ResultColumn]
    public DateTime datetime { get; set; }

(You don't need to type the name if the property name matches the field name)

Upvotes: 1

kagundajm
kagundajm

Reputation: 1200

To force correct column types, I would recommend using scripts to initialize the tables in the database and leave PetaPoco to perform CRUD operations. This is evident in the tests being used in PetaPoco where scripts are used for initialization as follows:

SQL Server

 CREATE TABLE petapoco (
   id bigint IDENTITY(1,1) NOT NULL,
   date_created datetime NOT NULL,
   ...
 );

mysql

CREATE TABLE petapoco (
   id bigint AUTO_INCREMENT NOT NULL,
   date_created datetime NOT NULL,
   ...
  ) ENGINE=INNODB;

The column decorator is used to change the column name to which a property is mapped by specifying it as an argument to the [column] attribute. Therefore if you have a property named id but the column name in the table is named article_id, you would use the following to do the mappings:

[Column("article_id")] 
public long id { get; set; }

In your code you would be using the property name while saving and fetching data and PetaPoco will map the property to the correct column.

So in your question, there would be no need for the attribute as the property and the column names are identical.

Upvotes: 0

Related Questions