我通过SQL Server Tuning Advisor运行了几个示例,它推荐了一个索引,但是我不太确定如何首先用代码实现它们。
public class Exchange
{
public int Id { get; set; }
public int ExchangeSetId { get; set; }
[Required]
[MaxLength(5)]
public string AreaCode { get; set; }
[Required]
[MaxLength(10)]
public string BasePrefix { get; set; }
[MaxLength(100)]
public string BaseLabel { get; set; }
[MaxLength(100)]
public string BaseLocation { get; set; }
[Required]
[MaxLength(10)]
public string DestPrefix { get; set; }
[MaxLength(100)]
public string DestLabel { get; set; }
[MaxLength(100)]
public string DestLocation { get; set; }
[MaxLength(100)]
public string Rule { get; set; }
public int ClassId { get; set; }
}
Index Tuning Advisor建议如下
SET ANSI_PADDING ON
CREATE NONCLUSTERED INDEX [_dta_index_Exchanges_5_661577395__K3_K4_K7] ON [dbo].[Exchanges]
(
[AreaCode] ASC,
[BasePrefix] ASC,
[DestPrefix] ASC
)WITH (SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF) ON [PRIMARY]
我如何将其添加到我的数据库上下文/模型?
非常感谢任何帮助提示。
答案 0 :(得分:0)
由于您使用的是EF 6.1,因此可以使用IndexAttribute。您将其添加到属性:
[Index("IndexName", 1)]
[Required]
[MaxLength(5)]
public string AreaCode { get; set; }
[Index("IndexName", 2)]
[Required]
[MaxLength(10)]
public string BasePrefix { get; set; }
[Index("IndexName", 3)]
[Required]
[MaxLength(10)]
public string DestPrefix { get; set; }
如果您使用迁移,则需要使用CreateIndex()
选项添加它。