How can I set multicolumn index on model like that:
public class Meta
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public Guid Id { get; set; }
[Index("MetaPeriodDateUnq", IsUnique = true, Order = 2)]
[Required]
public DateTime Date { get; set; }
[Index("MetaPeriodDateUnq", IsUnique = true, Order = 1)]
[Required]
public virtual PeriodType Period { get; set; }
/*
...
*/
}
public class PeriodType
{
[Key]
public Guid Id { get; set; }
/*
...
*/
}
After DB initialization there is only "MetaPeriodDateUnq" index mentioning Meta.Date column, but I'm relying on Meta.Date + Meta.Period.Id uniqueness.
You must include the foreign key explicitly, annotations on navigation properties do generally not work.
[Index("MetaPeriodDateUnq", IsUnique = true, Order = 2)]
public DateTime Date { get; set; }
[Index("MetaPeriodDateUnq", IsUnique = true, Order = 1)]
public Guid PeriodId { get; set; }
public virtual PeriodType Period { get; set; }
This should work (not tested).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With