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.
C# SQLite-net define multi column unique
Asked Answered
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.
Thanks! I was just looking for this. What is the syntax for having multiple indices on the same field (a compound index like yours, and another index on same field but by itself)? Everything I try is getting a duplicate index error from the compiler. –
Mchail
@RobertOschler Found this issue on github and from there got the workaround for doing multiple indices on the same field. Are you
roschler
? –
Sfumato Here's another solution
public string ListingNumber { get; set; }
public string ChannelCode { get; set; }
[Unique]
public string UniqueChannelCodeListingNumber => $"{ListingNumber}_{ChannelCode}";
© 2022 - 2025 — McMap. All rights reserved.