首先在EFCore 3.1.1代码中播种数据之后,是否可以将身份值设置为最后插入的ID + 1?

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

因此,我分别以ID的1,2,3和4播种了一些Identity Server用户。在运行应用程序后尝试插入新用户后,我必须“尝试”插入4次,然后将ID设置为5,然后我才能开始无误地插入。

我曾尝试寻找答案,但很短。问题-通过种子植入后使用ModelBuilder或查询数据库,这种可行的低谷代码优先方法吗?

此外,我在Asp Net Core应用程序中以代码优先方法使用PostgreSQL数据库。

编辑:另外,这是所有ID列约束,它们表明它是一个标识列。

enter image description here

c# entity-framework-core code-first
1个回答
0
投票

我发现比在数据库条目上设置负值更正确的解决方法。

// Set starting value to start after seeded data count. Must be done
// to any entity, that can be added to from application logic.
builder.Entity<SomeTable>()
    .Property(b => b.YourIdProperty)
    .HasIdentityOptions(startValue: seededDataCount + 1);

这是针对postgreSQL的,但对于其他实现应该确实类似。

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