Search code examples
c#entity-frameworknpgsql

42703: column b.Id does not exist


I have a naive EF implementation with Postgresql which is returning me the error

{"42703: column b.Id does not exist\r\n\r\nPOSITION: 8"}. Perhaps you meant to reference the column "b.id".

The code is as simple as

await _dbContext.BlackList.FindAsync(1);

The model is defined as

 [Table("blacklisted_people", Schema ="public")]
 public class BlackListedPeople
 {
     [Key]
     public int Id { get; set; }
    
 }

The db context is defined as

public class ApplicationDbContext:DbContext
 {
     public ApplicationDbContext(DbContextOptions<ApplicationDbContext> dbContextOptions)
         : base(dbContextOptions)
     {}

     public DbSet<BlackListedPeople> BlackList { get; set; }
 }

The table is defined in postgresql as

create table blacklisted_people
(
    id integer not null primary key
)

Solution

  • By default, EF maps column names to the exact .NET property name, so it generates SQL expecting an Id column in the database, but your actual column name is id.

    You can manually configure the property's column name (EF docs), or possibly try to use the EFCore.NamingConventions if the naming translation is systematic snake-case, for example.