为什么实体框架 4.0 需要使用 DTM?可以不使用吗?

发布于 2024-09-30 10:11:55 字数 964 浏览 0 评论 0原文

我大约一年前使用 EF。我只是尝试使用 TransactionScope 处理相当复杂的查询,如下面的代码。

using (var tran = new TransactionScope())
{
    // Clear all data in some table
    DataContext.SomeTables
        .ForEach(x => DataContext.SomeTables.DeleteObject(x));

    DataContext.SaveChanges();

    // Import data from excel as DataSet object.
    var ds = ImportDataFromExcel(file.FullName);

    foreach (DataRow dr in ds.Tables[0].Rows)
    {
        DataContext.SomeTables.AddObject(new SomeTable
        {
            // fill object with data from current row.
        });
    }

    DataContext.SaveChanges();

    // Commit Transaction
    tran.Complete();
}

之后,我收到一些有关 DTM 未启用或不可访问的错误。我知道这是由于数据库服务器上的此服务未启动或被防火墙阻止所致。但这不是我的观点。

我想知道为什么 EF 使用此服务来创建事务,而普通 SQL 脚本只能使用“BEGIN TRAN”、“SAVE TRAN”或“ROLLBACK TRAN”等语句来执行此操作。

有没有其他方法可以避免为我非常简单的事务语句调用 DTM 服务?

附言。当前的工作数据库很小,我认为它不应该受到不同事务技术的影响。

谢谢

I use EF almost one year ago. I just try to use TransactionScope deal with quite complex query like the following code.

using (var tran = new TransactionScope())
{
    // Clear all data in some table
    DataContext.SomeTables
        .ForEach(x => DataContext.SomeTables.DeleteObject(x));

    DataContext.SaveChanges();

    // Import data from excel as DataSet object.
    var ds = ImportDataFromExcel(file.FullName);

    foreach (DataRow dr in ds.Tables[0].Rows)
    {
        DataContext.SomeTables.AddObject(new SomeTable
        {
            // fill object with data from current row.
        });
    }

    DataContext.SaveChanges();

    // Commit Transaction
    tran.Complete();
}

After that, I got some error about DTM is not enabled or accessible. I know it cause by this service at database server is not start or blocked by firewall. But it is not my point.

I want to know why EF uses this service to create transaction while plain SQL script can use only some statement like "BEGIN TRAN", "SAVE TRAN" or "ROLLBACK TRAN" does that.

Is there any other way to avoid calling DTM service for my quite simple transaction statement?

PS. Current working database is so small and I think it should not be affect from different transaction technic.

Thanks,

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

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

发布评论

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

评论(1

北音执念 2024-10-07 10:11:55

在我搜索了很多网站之后,我发现我可以使用实体事务手动创建事务,如下源代码所示。

using (var tran = DataContext.BeginTransaction())
{
    // Clear all data in some table
    DataContext.SomeTables
        .ForEach(x => DataContext.SomeTables.DeleteObject(x));

    DataContext.SaveChanges();

    // Import data from excel as DataSet object.
    var ds = ImportDataFromExcel(file.FullName);

    foreach (DataRow dr in ds.Tables[0].Rows)
    {
        DataContext.SomeTables.AddObject(new SomeTable
        {
            // fill object with data from current row.
        });
    }

    DataContext.SaveChanges();

    // Commit Transaction
    tran.Commit();
}

Helper 类

public static DbTransaction BeginTransaction(this ObjectContext context, IsolationLevel isolationLevel = IsolationLevel.ReadCommitted)
{
    if (context.Connection.State != ConnectionState.Open)
    {
        context.Connection.Open();
    }

    return context.Connection.BeginTransaction(isolationLevel);
}

感谢 Kim Major 回答以下问题。我找不到任何其他页面明确建议我使用实体事务,包括主 MSDN 网站 (如何:管理实体框架中的事务)。

如何通过实体框架使用事务?

After I search a lot of websites, I just found I can manually create transaction using Entity transaction like the following source code.

using (var tran = DataContext.BeginTransaction())
{
    // Clear all data in some table
    DataContext.SomeTables
        .ForEach(x => DataContext.SomeTables.DeleteObject(x));

    DataContext.SaveChanges();

    // Import data from excel as DataSet object.
    var ds = ImportDataFromExcel(file.FullName);

    foreach (DataRow dr in ds.Tables[0].Rows)
    {
        DataContext.SomeTables.AddObject(new SomeTable
        {
            // fill object with data from current row.
        });
    }

    DataContext.SaveChanges();

    // Commit Transaction
    tran.Commit();
}

Helper class

public static DbTransaction BeginTransaction(this ObjectContext context, IsolationLevel isolationLevel = IsolationLevel.ReadCommitted)
{
    if (context.Connection.State != ConnectionState.Open)
    {
        context.Connection.Open();
    }

    return context.Connection.BeginTransaction(isolationLevel);
}

Thank to Kim Major for answering the following question. I cannot found any other pages that clearly suggest me to use Entity transaction including main MSDN website (How to: Manage Transactions in the Entity Framework).

How to use transactions with the Entity Framework?

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