Search code examples
c#.netservicestackormlite-servicestack

ServiceStack OrmLite: Use default database constraint instead of null value from data model


I'm still pretty new to these technologies. I've run into a small issue, and it's one that can be fixed by writing some lazy code...but OrmLite and ServiceStack streamline so many things, I'm wondering if there's a better way to do this.

So, I have a data model:

public class cctv_camera
{
    [AutoIncrement]
    public int I_id { get; set; }
    public string I_sid { get; set; }
    public string C_store_id { get; set; }
    // .... others
}

This data model is mapped to a table, cctv_camera. There's another model (call it CamDetail) being sent to the client after some joins from this table. We are receiving back a CamDetail object from the client on a POST to save to the database and populating an instance of lp_cctv_camera with the data (new lp_cctv_camera().PopulateWith(CamDetail);).

Here's the thing: the I_sid column is a NOT NULL column with a default constraint that generates a hash for that row. It's something that the database is responsible for, so new items should not INSERT this column; it should be generated by the constraint.

Is there any way to db.Insert(lp_cctv_camera) while ignoring this column? I have tried the [Ignore] attribute on the definition, but we still need it in the definition to send existing I_sids out to the client. I really can't find anything in the docs. Any help is appreciated!


Solution

  • We've added an explicit [IgnoreOnInsert] attribute you can use to ignore specific properties on Insert which is available on v4.5.13 on MyGet.

    Prior to v4.5.13 you can use the [Compute] attribute to get the similar behavior and ignore fields during inserts, e.g:

    public class cctv_camera
    {
        [AutoIncrement]
        public int I_id { get; set; }
        [Compute]
        public string I_sid { get; set; }
        public string C_store_id { get; set; }
        // .... others
    }