abp icon indicating copy to clipboard operation
abp copied to clipboard

Migration: filter: "[TenantId] IS NOT NULL"

Open 632374118 opened this issue 1 year ago • 4 comments

migrationBuilder.CreateIndex( name: "IX_AppDeviceCheckRecords_Id_TenantId", table: "AppDeviceCheckRecords", columns: new[] { "Id", "TenantId" }, unique: true, filter: "[TenantId] IS NOT NULL");

How is this filter automatically added?I couldn't find it in the ABP source code。

632374118 avatar May 16 '24 01:05 632374118

https://github.com/abpframework/abp/blob/84396c21ae3fa723be968d2ddb045b8e946b06fe/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs#L702 https://github.com/abpframework/abp/blob/84396c21ae3fa723be968d2ddb045b8e946b06fe/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityTypeBuilderExtensions.cs#L25 https://learn.microsoft.com/en-us/ef/core/querying/filters

maliming avatar May 16 '24 02:05 maliming

Thank you. I have other question: Assuming an entity has an ID, Code, and Name that uses soft deletion functionality. I set the code as a unique index. After I soft delete the data with Code="A", the user cannot query the data with Code="A" and cannot add the data with Code="A" anymore. The user will have questions. How can I solve them? According to your above answer, ISoftDelete also automatically added a query filter, but it did not affect the index.

632374118 avatar May 16 '24 02:05 632374118

builder.Entity<DeviceEntity>(b => { b.HasIndex(x => new { x.Id, x.TenantId }).IsUnique(); b.HasIndex(x => new { x.Code, x.TenantId }).IsUnique(); });

migrationBuilder.CreateIndex( name: "IX_AppDevices_Code_TenantId", table: "AppDevices", columns: new[] { "Code", "TenantId" }, unique: true, filter: "[TenantId] IS NOT NULL");

632374118 avatar May 16 '24 02:05 632374118

I understand now. If a field is of a nullable value type and an index is set, a filter will be automatically added during migration

632374118 avatar May 16 '24 02:05 632374118