当事务类型为 Table时,LINQ to SQL 事务

发布于 2024-11-18 17:25:14 字数 881 浏览 1 评论 0原文

我正在尝试创建一个事务来包装多个 LINQ to SQL SubmitChanges() 调用。

代码

System.Data.Common.DbTransaction trans = null;

using (DbContext context = new DbContext())
{
    context.Connection.Open()
    trans = context.Connection.BeginTransaction();

    context.Transaction = trans; // <-- ERROR HERE: Cannot be done!

    .... // Several calls to delete objects with context.SaveChanges()

    trans.Commit();
}

问题

在我们的数据库模式(它早于我,并且与应用程序紧密相关)中,我们有一个名为 Transaction 的表。

这意味着上面代码中的 Transaction 属性实际上不是 IDbTransaction 类型,并且我无法将 trans 对象分配给 context.Transaction。 context.Transaction 实际上是 Table 类型。

问题

鉴于我有一个名为 Transaction 的表,这一事实当前不可更改,如何将事务分配给我的上下文?

尽管我熟悉 LINQ to SQL,但这是我第一次在多个调用周围使用事务,因此我也准备好接受这样的事实:我一开始可能没有以正确的方式执行此操作,并且冲突的表名甚至可能不是问题......

I'm attempting to create a transaction to wrap around several LINQ to SQL SubmitChanges() calls.

Code

System.Data.Common.DbTransaction trans = null;

using (DbContext context = new DbContext())
{
    context.Connection.Open()
    trans = context.Connection.BeginTransaction();

    context.Transaction = trans; // <-- ERROR HERE: Cannot be done!

    .... // Several calls to delete objects with context.SaveChanges()

    trans.Commit();
}

Problem

In our database schema (which pre-dates me, and is heavily tied into the application), we have a table called Transaction.

This means that the Transaction property in the code above is not actually an IDbTransaction type, and I am unable to assign the trans object to context.Transaction. context.Transaction is actually of type Table<Transaction>.

Question

How can I assign a transaction to my context, given the currently unchangeable fact that I have a table named Transaction?

Although I'm familiar with LINQ to SQL, this is the first time for me using a Transaction around multiple calls, so I'm also ready to accept that I may not be doing this the right way in the first place, and that the conflicting table name may not even be an issue...

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

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

发布评论

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

评论(1

指尖上的星空 2024-11-25 17:25:14

如果 DataContext 中的 Transaction 属性被您的 DbContext 类隐藏,只需转换:

((DataContext) context).Transaction = trans;

这样 Transaction 就可以解析由编译器转换为 DataContext.Transaction 而不是 DbContext.Transaction

或者,您可以使用单独的变量,如果您要进行多次此类调用,这可能会很有用:(

DataContext vanillaContext = context;
vanillaContext.Transaction = trans;

我不知道这是否是使用事务的正确方法 - 这只是绕过您的方法命名冲突。)

If the Transaction property from DataContext is being hidden by your DbContext class, just cast:

((DataContext) context).Transaction = trans;

That way Transaction is resolved by the compiler to DataContext.Transaction instead of DbContext.Transaction.

Alternatively you could use a separate variable, which could be useful if you have several such calls to make:

DataContext vanillaContext = context;
vanillaContext.Transaction = trans;

(I have no idea whether or not this is the right way to use the transaction by the way - it's just the way to get around your naming collision.)

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