在实体框架中检索身份值并且仍然能够回滚

发布于 2024-11-27 03:53:58 字数 171 浏览 3 评论 0原文

我想在使用实体框架在数据库中插入行后检索标识列值。所以我必须调用 context.savechanges() ,但如果在更新行和检索标识列值后出现问题,我想完全回滚。

在 EF 4.0 中这是否可能? 我知道如何检索标识列值,并且通过回滚我的意思是应该删除插入的行。 我的身份列是自动生成的 bigint 类型。

I want to retrieve the Identity column value after inserting a row in DB using entity framework. So I have to call context.savechanges(), but in case something goes wrong after updating row and retrieving identity column value, I want to to rollback completely.

Is this possible somehow in EF 4.0.?
I know how to retrieve identity column value and by rollback I mean that the inserted row should be deleted.
My identity column is an autogenerated bigint type.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨落星ぅ辰 2024-12-04 03:53:59

使用TransactionScope怎么样?

using (var scope = new TransactionScope())
{
    using (var context = new MyContext())
    {
        // Insert data

        context.SaveChanges();

        // Do something with retrieved Id
    }

    // If something goes wrong and following command is not called
    // transaction will rollback
    scope.Complete(); // Commit
}

如果不调用Complete事务将会回滚。

What about using TransactionScope

using (var scope = new TransactionScope())
{
    using (var context = new MyContext())
    {
        // Insert data

        context.SaveChanges();

        // Do something with retrieved Id
    }

    // If something goes wrong and following command is not called
    // transaction will rollback
    scope.Complete(); // Commit
}

If you do not call Complete transaction will rollback.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文