首页 > 解决方案 > 在 ORACLE INSERT 之后使用 Entity Framework Core 获取新 ID

问题描述

我的实体模型是由 entityframework 核心脚手架生成的

在使用 Entity Framework Core 成功插入 Oracle 后,我阅读了一些帖子并没有获得新的 ID。到目前为止,这是我从帖子中复制的代码

var result = await this.myDbContent.MyEntity.AddAsync(myEntity);
var result1 = await this.myDbContent.SaveChangesAsync();
await transaction.CommitAsync();

var myId = this.myDbContent.Entry(myEntity).GetDatabaseValues(); // get null return

return myEntity.myId; // is 0

我检查了数据库并看到了新记录,但我无法检索新 ID。

在添加之前,它是 0,并且从未使用新的 id 值进行更新。

任何帮助表示赞赏。谢谢

标签: oracleasp.net-coreentity-framework-core

解决方案


在这里找到了答案,它解释了如何将 DatabaseGeneratedAttribute 与 EntityFrameWork 一起使用

https://www.learnentityframeworkcore.com/configuration/data-annotation-attributes/databasegenerated-attribute


推荐阅读