I am looking for a way to perform some slightly more intelligent unique constraints on my EF Code First database table.
I have a two models in play; A Setting and a Tenant.
public class Setting
{
public int Id { get; set; }
public string Name { get; set; }
public string Value { get; set; }
public int TenantId { get; set; }
}
public class Tenant
{
///
public int Id { get; set; }
public virtual List<Setting> Settings { get; set; }
///
}
As you can probably guess, a Setting is linked to it's Tenant by the TenantId property which acts as the foreign key and match the Tenant's Id property.
What I want to achieve is to say that the Setting name must be unique per Tenant. For example, you can have a Setting named "SettingA" under "Tenant01" and a "SettingA" under "Tenant02" (each of which are two separate entities with here own Ids in the table, and can thus have different Values) and this be legal, but not allow for another Setting name "SettingA" to the Tenant's where it already exists.
I can see plenty of ways to just enforce straight uniqueness through the likes of database initialisation and checking when adding etc, but nothing on "conditional uniqueness".