更新:我已经解决了这个问题。我的解决方案在我的迁移中丢失了,并且将针对您的数据库管理器。实体框架核心将空值分配给非空主键
解决方法是该线路的迁移:
Id = table.Column<long>(nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn)
我有我试图让EF处理ID生成的表。 Id很长。
这个模型看起来像:
public virtual long Id { get; set; }
public virtual DateTime Created { get; set; }
迁移的样子:
migrationBuilder
.CreateTable("MyEntity",
table => new
{
Id = table.Column<long>(nullable: false),
Created = table.Column<DateTime>(nullable: false)
}
模型快照的样子:
modelBuilder.Entity("MyEntity", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd();
b.Property<DateTime>("Created");
b.HasKey("Id");
}
当创建一个新的实体,而不是设置Id,Id的默认值为0.当将该值插入dataContext.MyEntities.Add(newEntity)
和i发现它现在具有非常大的负值的实体(似乎是负的long.MaxValue)。问题是在EF的dataContext.SaveChanges();
期间似乎将这个负值转换为空值并抛出。
模式:
CREATE TABLE "MyEntity" (
"Id" bigint NOT NULL,
"Created" timestamp without time zone NOT NULL,
);
您可以发布生成的表设计/架构 – DaniDev
实现每个数据库供应商不同。这是哪一个? –
这是针对postgres,使用“Npgsql.EntityFrameworkCore.PostgreSQL”:“1.1.0”, “Npgsql.EntityFrameworkCore.PostgreSQL.Design”:“1.1.0” – user2793442