简体   繁体   English

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

[英]How to define a multi-field index with Entity Framework Core 2.1

I am working on an ASP.Net Core 2.1 API and I use Entity Framework Core 2.1.我正在研究 ASP.Net Core 2.1 API,我使用 Entity Framework Core 2.1。 I am using migrations to manage changes to the database.我正在使用迁移来管理对数据库的更改。 My backing data store is an instance of Azure SQL Server.我的后备数据存储是 Azure SQL Server 的一个实例。

I need to add a multi-field non-clustered index to one of my tables but I am having difficulty finding a concise reference on how to do this in my google searches.我需要将多字段非聚集索引添加到我的一个表中,但我很难在我的 google 搜索中找到有关如何执行此操作的简明参考。

I tried using the [Index()] data annotation in the POCO class but it was not recognized.我尝试在 POCO 类中使用 [Index()] 数据注释,但无法识别。 So, I assume I have to do this in the OnModelCreating method of my DbContext class, but I have not found an example of how to do that for a multi-field non-clustered index.因此,我假设我必须在 DbContext 类的 OnModelCreating 方法中执行此操作,但我还没有找到有关如何为多字段非聚集索引执行此操作的示例。

Here is an example entity class这是一个示例实体类

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>();

}

And here is the entitybase class这是实体基类

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; }

}

I want to create a non-clustered index for ShippingServicesFileId and DeleteFlag in the OnModelCreating method of my DbContext do that it gets picked up when I run add-migration in my Package Manager Console.我想在我的 DbContext 的 OnModelCreating 方法中为 ShippingServicesFileId 和 DeleteFlag 创建一个非聚集索引,当我在我的包管理器控制台中运行 add-migration 时,它会被拾取。

Any ideas?有任何想法吗?

You cannot create an index on more than one column with data annotations so you have to use Fluent API (in OnModelCreating):您不能在具有数据注释的多个列上创建索引,因此您必须使用 Fluent API(在 OnModelCreating 中):

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

to create a non-clustered index.创建非聚集索引。 Use .IsUnique() to create a unique one.使用 .IsUnique() 创建一个唯一的。 Use .ForSqlServerIsClustered() if you want a clustered index on a SQL Server.如果您想在 SQL Server 上使用聚集索引,请使用 .ForSqlServerIsClustered()。 Optionally you can give it another name with .HasName("...").您可以选择使用 .HasName("...") 为其指定另一个名称。

You can pass in the columns as a string array during a migration if that's your preferred method.如果这是您的首选方法,您可以在迁移期间将列作为字符串数组传入。 Eg例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Elasticsearch索引多字段元素 - How to index multi-field element with elasticsearch Entity Framework Core 2.1中的ReverseEngineeringGenerator - ReverseEngineeringGenerator in Entity Framework Core 2.1 "如何在 Entity Framework Core 中定义外键" - How to define foreign Key in Entity Framework Core 如何在Entity Framework Core 2.1中映射SQL_VARIANT? - How to map SQL_VARIANT in Entity Framework Core 2.1? 在Entity Framework 6.1(非Core)中,如何使用IndexAttribute定义聚簇索引? - In Entity Framework 6.1 (not Core), how can I use the IndexAttribute to define a clustered index? 带有DetachedLazyLoadingWarning的实体框架核心2.1问题 - Entity Framework Core 2.1 problem with DetachedLazyLoadingWarning 将 SQL 查询转换为 Entity Framework Core 2.1 - Translate SQL query into Entity Framework Core 2.1 Entity Framework Core 2.1中的种子关系数据 - Seeding relationship data in Entity Framework Core 2.1 Entity Framework Core 2.1 - 多个提供者 - Entity Framework Core 2.1 - Multiple Providers MySqlException:字段 'id' 没有默认值 - Entity Framework Core 2.1 - MySqlException: Field 'id' doesn't have a default value - Entity Framework Core 2.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM