如何使用Entity Framework Core 2.1定义多字段索引

时间:2018-07-23 16:31:00

标签: c# sql-server .net-core entity-framework-core asp.net-core-2.0

我正在使用ASP.Net Core 2.1 API,并且使用Entity Framework Core 2.1。我正在使用迁移来管理对数据库的更改。我的后备数据存储区是Azure SQL Server的实例。

我需要向其中一个表中添加一个多字段非聚集索引,但是我很难在Google搜索中找到有关如何执行此操作的简明参考。

我尝试在POCO类中使用[Index()]数据注释,但无法识别。因此,我假设我必须在DbContext类的OnModelCreating方法中执行此操作,但是我没有找到针对多字段非聚集索引执行此操作的示例。

这是示例实体类

public class H1Record : EntityBase
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public long Id { get; set; }

    [ForeignKey("ShippingServicesFileId")]
    public ShippingServicesFile ShippingServicesFile { get; set; }

    [Required]
    public int ShippingServicesFileId { get; set; }

    [Column(TypeName = "varchar(20)")]
    public string BatchId { get; set; }

    [Column(TypeName = "varchar(34)")]
    [MaxLength(34)]
    public string ElectronicFileNumber { get; set; }

    [Column(TypeName = "varchar(1)")]
    [MaxLength(1)]
    public string ElectronicFileType { get; set; }

    [Column(TypeName = "varchar(8)")]
    [MaxLength(8)]
    public string DateOfMailing { get; set; }

    [Column(TypeName = "varchar(6)")]
    [MaxLength(6)]
    public string TimeOfMailing { get; set; }

    public DateTime MailingDateTime { get; set; }

    [Column(TypeName = "varchar(1)")]
    [MaxLength(1)]
    public string EntryFacilityType { get; set; }

    [Column(TypeName = "varchar(5)")]
    [MaxLength(5)]
    public string EntryFacilityZipCode { get; set; }

    [Column(TypeName = "varchar(4)")]
    [MaxLength(4)]
    public string EntryFacilityZipPlus4 { get; set; }

    [Column(TypeName = "varchar(2)")]
    [MaxLength(2)]
    public string DirectEntryOriginCountryCode { get; set; }

    [Column(TypeName = "varchar(3)")]
    [MaxLength(3)]
    public string ShipmentFeeCode { get; set; }

    [Column(TypeName = "varchar(6)")]
    [MaxLength(6)]
    public string ExtraFeeForShipment { get; set; }

    [Column(TypeName = "varchar(2)")]
    [MaxLength(2)]
    public string ContainerizationIndicator { get; set; }

    [Column(TypeName = "varchar(3)")]
    [MaxLength(3)]
    public string UspsElectronicFileVersionNumber { get; set; }

    [Column(TypeName = "varchar(12)")]
    [MaxLength(12)]
    public string TransactionId { get; set; }

    [Column(TypeName = "varchar(8)")]
    [MaxLength(8)]
    public string SoftwareVendorProductVersionNumber { get; set; }

    [Column(TypeName = "varchar(9)")]
    [MaxLength(9)]
    public string FileRecordCount { get; set; }

    [Column(TypeName = "varchar(9)")]
    [MaxLength(9)]
    public string MailerId { get; set; }

    public ICollection<D1Record> D1Records { get; set; } = new List<D1Record>();

    public ICollection<C1Record> C1Records { get; set; } = new List<C1Record>();

}

这是实体库类

public class EntityBase
{
    public DateTime CreatedDate { get; set; }
    public DateTime LastModifiedDate { get; set; }
    public int CreatedByUserId { get; set; }
    public int LastModifiedByUserId { get; set; }
    public bool DeleteFlag { get; set; }

}

我想在DbContext的OnModelCreating方法中为ShippingServicesFileId和DeleteFlag创建一个非聚集索引,这样做是在程序包管理器控制台中运行add-migration时得到的。

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

您不能在一个以上带有数据注释的列上创建索引,因此必须使用Fluent API(在OnModelCreating中):

modeBuilder.Entity<ENTITYCLASS>().HasIndex(x => new {x.PROPERTY1, x.PROPERTY2, ...})

创建一个非聚集索引。使用.IsUnique()创建一个唯一的。如果要在SQL Server上使用聚簇索引,请使用.ForSqlServerIsClustered()。 (可选)您可以使用.HasName(“ ...”)为它命名。

答案 1 :(得分:0)

如果这是您的首选方法,则可以在迁移过程中将其作为字符串数组传入列。例如

migrationBuilder.CreateIndex("IX_H1Record", "H1Record",new string[] { "ShippingServicesFileId", "DeleteFlag"}, "dbo");