【问题标题】:How can I stop EF Core from creating a filtered index on a nullable column如何阻止 EF Core 在可为空的列上创建筛选索引
【发布时间】:2018-06-10 07:57:59
【问题描述】:

我有这个模型:

public class Subject
{
    public int Id { get; set; }

    [Required]
    [StringLength(50)]
    public string Name { get; set; }

    public int LevelId { get; set; }

    [ForeignKey("LevelId")]
    public Level Level { get; set; }

    [Column(TypeName = "datetime2")]
    public DateTime? DeletedAt { get; set; }
}

并通过 Fluent API 配置索引:

entityBuilder.HasIndex(e => new { e.LevelId, e.Name, e.DeletedAt })
    .IsUnique();

它正在创建一个具有唯一过滤索引的表。如何防止 EF 添加过滤器?我只想要索引而不希望它被过滤。

【问题讨论】:

    标签: entity-framework entity-framework-core ef-core-2.0


    【解决方案1】:

    创建不包括 NULL 值的过滤索引是包含可为空列的唯一索引的默认 EF Core 行为。

    您可以使用HasFilter fluent API 更改过滤条件或通过将null 作为sql 参数将其关闭:

    entityBuilder.HasIndex(e => new { e.LevelId, e.Name, e.DeletedAt })
        .IsUnique()
        .HasFilter(null);
    

    【讨论】:

    • 注意.HasFilter(null)必须出现在.IsUnique()之后,否则仍然会生成过滤器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-08
    • 2021-05-23
    • 1970-01-01
    • 2011-08-13
    • 2014-09-05
    相关资源
    最近更新 更多