TransactionScope 与依赖对象上的对象上下文

发布于 2024-12-04 16:32:39 字数 1473 浏览 4 评论 0 原文

我正在开发 MVC3 应用程序,并且正在使用链接到 Oracle 数据库 (11G R2) 的实体框架。 当我尝试在 TransactionScope 内使用单个对象上下文时遇到问题。

这是代码:

using (TransactionScope scope = new TransactionScope())
{
    using (Entities context = new Entities())
    {
        // Right insert
        T_RIGRIGHT entity1 = new T_RIGRIGHT()
        {
            RIGCODE = "test1",
            RIGINSERTLOGIN = "aco",
            RIGINSERTDATE = DateTime.Now,
            RIGUPDATELOGIN = "aco",
            RIGUPDATEDATE = DateTime.Now
        };

        context.AddToT_RIGRIGHT(entity1);

        context.SaveChanges();

        // Right/Profile insert
        T_RIPRIGHTPROFILE entity2 = new T_RIPRIGHTPROFILE()
        {
            PROID = 3,
            RIGID = entity1.RIGID,
            RIPINSERTLOGIN = "aco",
            RIPINSERTDATE = DateTime.Now,
            RIPUPDATELOGIN = "aco",
            RIPUPDATEDATE = DateTime.Now
        };

        context.AddToT_RIPRIGHTPROFILE(entity2);

        context.SaveChanges(); // SaveChanges fails due to the FK constraint on table 
    }

    scope.Complete();
}

让我解释一下代码...

首先,我创建一个名为 entity1 的实体作为 T_RIGRIGHT 元素。 我实例化一个 T_RIPRIGHTPROFILE 元素,该元素使用之前创建的 T_RIGRIGHT 元素的 id。

第二个 context.SaveChanges() 执行失败,异常涉及表 T_RIPRIGHTPROFILE 上的外键约束(需要 T_RIGRIGHT)。

希望我的解释足够清楚

有什么办法让它发挥作用吗?

PS:我为我的英语道歉,因为它不是我的母语。

I'm working on a MVC3 application and i'm using the Entity Framework linked to an Oracle database (11G R2).
I'm encountering an issue when i'm trying to use a single object context inside a TransactionScope.

Here is the code :

using (TransactionScope scope = new TransactionScope())
{
    using (Entities context = new Entities())
    {
        // Right insert
        T_RIGRIGHT entity1 = new T_RIGRIGHT()
        {
            RIGCODE = "test1",
            RIGINSERTLOGIN = "aco",
            RIGINSERTDATE = DateTime.Now,
            RIGUPDATELOGIN = "aco",
            RIGUPDATEDATE = DateTime.Now
        };

        context.AddToT_RIGRIGHT(entity1);

        context.SaveChanges();

        // Right/Profile insert
        T_RIPRIGHTPROFILE entity2 = new T_RIPRIGHTPROFILE()
        {
            PROID = 3,
            RIGID = entity1.RIGID,
            RIPINSERTLOGIN = "aco",
            RIPINSERTDATE = DateTime.Now,
            RIPUPDATELOGIN = "aco",
            RIPUPDATEDATE = DateTime.Now
        };

        context.AddToT_RIPRIGHTPROFILE(entity2);

        context.SaveChanges(); // SaveChanges fails due to the FK constraint on table 
    }

    scope.Complete();
}

Let me explain the code...

First I create an entity called entity1 as a T_RIGRIGHT element.
The I instanciate a T_RIPRIGHTPROFILE element that uses the id of the T_RIGRIGHT element created before.

The execution fails on the second context.SaveChanges() and the exception concerns the Foreign Key constraint on the table T_RIPRIGHTPROFILE (requires a T_RIGRIGHT).

Hope my explanations are clear enough

Is there any way to make it works ?

P.S. : I apologize for my english as it's not my native language.

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

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

发布评论

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

评论(1

娇妻 2024-12-11 16:32:39

您正在尝试分配尚未提交给数据库的实体的 FK entity1.RIGID

RIGID = entity1.RIGID,

如果您仔细观察entity1,您会发现 RIGID 为 0默认 - 相反,您应该设置表示 FK 关系的导航属性:

RIG = entity1,

这将使 EF 能够正确关联这些实体,因为该实体 1 尚未提交到数据库,因此您甚至不需要额外的 SaveChanges () 称呼。

此外,在您的场景中,您不需要 TransactionScope - EF 在 SaveChanges() 中使用内部已有的事务 - 根据建议的更改,您只需要一个 SaveChanges( ) 调用,因此不需要外部事务范围。

You are trying to assign the FK entity1.RIGID of an entity that has not been committed to the DB:

RIGID = entity1.RIGID,

If you look at entity1 closely you will see that RIGID is 0 by default - instead you should set the navigation property representing the FK relationship:

RIG = entity1,

This will enable EF to properly relate these entities, for this entity1 does not have to be committed to the DB yet, so you do not even need the extra SaveChanges() call.

Also in your scenario you do not need a TransactionScope - EF uses a transaction internally already in SaveChanges() - based on the suggested changes you only need one SaveChanges() call and hence no outer transaction scope is needed.

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