Donald French
Donald French

Reputation: 1801

C# SQLite-net define multi column unique

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.

Upvotes: 21

Views: 9138

Answers (2)

Here's another solution

public string   ListingNumber { get; set; }
public string   ChannelCode { get; set; }
[Unique] 
public string UniqueChannelCodeListingNumber => $"{ListingNumber}_{ChannelCode}";

Upvotes: 0

Donald French
Donald French

Reputation: 1801

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.

Upvotes: 51

Related Questions