如何在不使用 TransactionScopes 的情况下使用 EF4 CodeFirst CTP5 分配事务?

发布于 2024-10-14 17:46:32 字数 1672 浏览 0 评论 0原文

我正在尝试对实时数据库执行功能测试,以确保我的 linq 查询正确转换为 SQL。我想通过使用事务来做到这一点,以便一个功能测试不会影响另一个功能测试。

问题是,我无法在 API 中找到任何正确使用事务的方法。我可以通过 MyDbContext.Database.Connection.BeginTransaction() 检索新的 DbTransaction,但是,我无论如何都找不到实际使用此事务的方法。

我能找到的有关 BeginTransaction() 调用的所有文档都是通过 cmd.Transaction 调用将事务对象分配给 SqlCommand您正在执行操作的命令。但是,对于 EF4 CTP5,无法访问用于查询的 SqlCommand

因此,我不知道如何使用通过 BeginTransaction() 开始的事务。

我不想使用 TransactionScope 对象,主要是因为 Sql CE 4 不支持它们,而且我更愿意使用本地数据库而不是通过网络。 Sql CE 4 确实支持通过 BeginTransaction() 检索事务,我只是无法首先弄清楚如何使用代码。

有谁知道如何做到这一点?


Edit: After some emails with Microsoft, it seems that TransactionScope() calls are meant to be the primary way to use transactions with EF4. To get TransactionScope to work with Sql CE you have to explicitely open the database connection prior to starting the transaction, for example

    [TestMethod]
    public void My_SqlCeScenario ()
    {
        using (var context = new MySQLCeModelContext()) //ß derived from DbContext
        {
            ObjectContext objctx = ((IObjectContextAdapter)context).ObjectContext;
            objctx.Connection.Open(); //ß Open your connection explicitly
            using (TransactionScope tx = new TransactionScope())
            {

                var product = new Product() { Name = "Vegemite" };
                context.Products.Add(product);
                context.SaveChanges();
            }
            objctx.Connection.Close(); //ß close it when done!
        }
    }

I am attempting to perform functional tests against a live database, to make sure my linq queries are translating into SQL correctly. I want to do this by using transactions, so that one functional test does not affect another.

The problem is, I cannot find any way in the API to utilize transactions correctly. I can retrieve a new DbTransaction via MyDbContext.Database.Connection.BeginTransaction(), however, I cannot find anyway to actually use this transaction.

All documentation I can find about the BeginTransaction() call, is you assign the transaction object to the SqlCommand via a cmd.Transaction call for the command you are performing actions with. However, with EF4 CTP5, there is no way to access the SqlCommand used for the queries.

Thus, I can't figure out how to use the transaction I have begun with BeginTransaction().

I do not want to use TransactionScope objects, mostly because Sql CE 4 does not support them, and I would prefer to use a local database and not go across a network. Sql CE 4 DOES support transactions retrieved via BeginTransaction(), I just can't figure out how with code first.

Does anyone have any idea how to do this?


Edit: After some emails with Microsoft, it seems that TransactionScope() calls are meant to be the primary way to use transactions with EF4. To get TransactionScope to work with Sql CE you have to explicitely open the database connection prior to starting the transaction, for example

    [TestMethod]
    public void My_SqlCeScenario ()
    {
        using (var context = new MySQLCeModelContext()) //ß derived from DbContext
        {
            ObjectContext objctx = ((IObjectContextAdapter)context).ObjectContext;
            objctx.Connection.Open(); //ß Open your connection explicitly
            using (TransactionScope tx = new TransactionScope())
            {

                var product = new Product() { Name = "Vegemite" };
                context.Products.Add(product);
                context.SaveChanges();
            }
            objctx.Connection.Close(); //ß close it when done!
        }
    }

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

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

发布评论

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

评论(1

晒暮凉 2024-10-21 17:46:32

这很俗气,但可能是您唯一的选择:

((EntityConnection)myObjectContext.Connection).StoreConnection.BeginTransaction(...

This is cheesy, but may be your only option here:

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