C# SQLite-net define multi column unique

Donald French picture Donald French · Sep 12, 2013 · Viewed 7.2k times · Source

I have seen references to changes in SQLite-net that supports multi-column unique constraints. I know it can be done directly with sqlite however I prefer to stay wit the sqlite-net methods of doing things. What is the Syntax to do multi-column unique. Single is [Unique] above the column desired to be unique.

Answer

Donald French picture Donald French · Sep 12, 2013

I have found the answer by reviewing the actual unit tests included in the project. It is base upon using the named parameters on the index attribute. For example:

    [Indexed(Name = "ListingID", Order = 2, Unique = true)]
    public string   ListingNumber { get; set; }
    [Indexed(Name = "ListingID", Order = 1, Unique = true)]
    public string   ChannelCode { get; set; }

will create an index named ListingID over two fields that must be unique. It you do not want the unique attribute, remove it as a parameter. You must use the named parameters to make it work. Also all field in an index must have the same Unique value.