42703:b.Id列不存在

问题描述 投票:0回答:1

我有一个使用 Postgresql 的简单 EF 实现,它返回错误

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

代码很简单

await _dbContext.BlackList.FindAsync(1);

模型定义为

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

数据库上下文定义为

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

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

该表在 postgresql 中定义为

create table blacklisted_people
(
    id integer not null primary key
)
c# entity-framework npgsql
1个回答
0
投票

对我来说,您的代码运行没有问题,让 EF Core 使用通常的代码行创建新表:

db.Database.EnsureDeleted();
db.Database.EnsureCreated();

主键是由integer类型创建的,自动增量不为空,甚至没有使用[Key]注释。

我使用了 Npgsql.EntityFrameworkCore.PostgreSQL 8.0.0 NuGet 包。

© www.soinside.com 2019 - 2024. All rights reserved.