如何阻止 EF Core 在可空列上创建过滤索引 [英] How can I stop EF Core from creating a filtered index on a nullable column

查看:27
本文介绍了如何阻止 EF Core 在可空列上创建过滤索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个模型:

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 配置的索引:

And index configured via Fluent API:

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

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

It's creating a table with a unique filtered index. How can I prevent EF from adding the filter? I just want the index and don't want it filtered.

推荐答案

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

Creating filtered index excluding NULL values is the default EF Core behavior for unique indexes containing nullable columns.

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

You can use HasFilter fluent API to change the filter condition or turn it off by passing null as sql argument:

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

这篇关于如何阻止 EF Core 在可空列上创建过滤索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆