Efcore.pg: CreateIndex with filter doesn't work

Created on 9 May 2018  路  4Comments  路  Source: npgsql/efcore.pg

modelBuilder.Entity<UserInfo>().HasIndex(e => e.UserId).HasFilter("IsActive=1").IsUnique();

it tells me that "the column "isactive" doesn't exist"

However it's there

public class UserInfo
{
    public int Id { get; set; }
    public bool IsActive { get; set; }
    public string Name { get; set; }
    public int UserId { get; set; }
    [ForeignKey("UserId")]
    public User User { get; set; }
}

Most helpful comment

Using escape sequence characters and double quotes.

modelBuilder.Entity<UserInfo>().HasIndex(e => e.UserId).HasFilter("\"IsActive\"=true").IsUnique();

I would recommend you to read 4.1.1. Identifiers and Key Words.

All 4 comments

In PostgreSQL, unquoted identifiers are folded to lowercase - that's why it's looking for isactive, all lowercase. Surround the column name with double quotes.

Also note that in PostgreSQL booleans aren't 1 and 0, they're true and false.

@roji How should I surround it with double quotes?

Try: .HasFilter("\"IsActive\"")

Using escape sequence characters and double quotes.

modelBuilder.Entity<UserInfo>().HasIndex(e => e.UserId).HasFilter("\"IsActive\"=true").IsUnique();

I would recommend you to read 4.1.1. Identifiers and Key Words.

Was this page helpful?
0 / 5 - 0 ratings