2 using System.Collections.Generic;
\r
5 using NUnit.Framework;
\r
9 using System.Data.Common;
\r
15 namespace Test_NUnit_MySql
\r
17 namespace Test_NUnit_OracleODP
\r
19 namespace Test_NUnit_Oracle
\r
21 namespace Test_NUnit_PostgreSql
\r
23 namespace Test_NUnit_Sqlite
\r
25 namespace Test_NUnit_Ingres
\r
26 #elif MSSQL && L2SQL
\r
27 namespace Test_NUnit_MsSql_Strict
\r
29 namespace Test_NUnit_MsSql
\r
31 namespace Test_NUnit_Firebird
\r
35 public class Transactions : TestBase
\r
38 public void Setup_LinqToSqlInsert07()
\r
40 Northwind db = CreateDB();
\r
42 from o in db.OrderDetails
\r
43 where o.Order.CustomerID == "WARTH"
\r
47 (from o in db.Orders
\r
48 where o.CustomerID == "WARTH"
\r
49 select o).FirstOrDefault();
\r
51 if (!orderDetails.Any() || order == null)
\r
53 var o = new Order { CustomerID = "WARTH", Employee = db.Employees.First() };
\r
54 o.OrderDetails.Add(new OrderDetail { Discount = 0.1f, Quantity = 1, Product = db.Products.First(p => p.ProductID % 2 == 0) });
\r
55 o.OrderDetails.Add(new OrderDetail { Discount = 0.2f, Quantity = 1, Product = db.Products.First(p => p.ProductID % 2 != 0) });
\r
56 db.Orders.InsertOnSubmit(o);
\r
61 #if !DEBUG && (SQLITE || (MSSQL && !L2SQL))
\r
65 public void TransactionRollbackDelete()
\r
67 Northwind db = CreateDB();
\r
68 DbTransaction t = BeginTransaction(db);
\r
73 from o in db.OrderDetails
\r
74 where o.Order.CustomerID == "WARTH"
\r
78 (from o in db.Orders
\r
79 where o.CustomerID == "WARTH"
\r
80 select o).FirstOrDefault();
\r
82 if (!orderDetails.Any() || order == null)
\r
83 Assert.Ignore("Preconditions");
\r
85 db.OrderDetails.DeleteAllOnSubmit(orderDetails); //formerly Remove(od);
\r
87 db.Orders.DeleteOnSubmit(order); //formerly Remove(order);
\r
91 db.OrderDetails.Any(od => od.Order.Customer.CustomerID == "WARTH" && od.Order.EmployeeID == 3));
\r
92 Assert.IsFalse(db.Orders.Any(ord => ord.OrderID == order.OrderID));
\r
100 private DbTransaction BeginTransaction(Northwind db)
\r
102 db.Connection.Open();
\r
103 DbTransaction t = db.Connection.BeginTransaction();
\r
104 db.Transaction = t;
\r
109 #if !DEBUG && (SQLITE || (MSSQL && !L2SQL))
\r
113 public void TransactionCheckAndRollbackInsert()
\r
115 Northwind db = CreateDB();
\r
116 DbTransaction t = BeginTransaction(db);
\r
118 var cust = new Customer();
\r
119 int beforeCustomersCount = db.Customers.Count();
\r
121 string id = new object().GetHashCode().ToString().Substring(0, 5);
\r
122 cust.CustomerID = id;
\r
123 cust.Country = "Spain";
\r
124 cust.CompanyName = "Coco";
\r
126 db.Customers.InsertOnSubmit(cust);
\r
127 db.SubmitChanges();
\r
129 int afterCustomercount = db.Customers.Count();
\r
130 Assert.IsTrue(beforeCustomersCount + 1 == afterCustomercount);
\r
134 afterCustomercount = db.Customers.Count();
\r
135 Assert.IsTrue(beforeCustomersCount == afterCustomercount);
\r
137 // The Count is correct. However, DataContext doesn't know that the
\r
138 // transaction was aborted, and will satisfy the following from
\r
139 // an internal cache
\r
140 var customer = db.Customers.FirstOrDefault(c => c.CustomerID == id);
\r
141 Assert.IsNotNull(customer);
\r
143 // Let's let DataContext know that it doesn't exist anymore.
\r
144 db.Customers.DeleteOnSubmit(customer);
\r
145 db.SubmitChanges(); // Note no exception from deleting a non-existent entity
\r
147 customer = db.Customers.FirstOrDefault(c => c.CustomerID == id);
\r
148 Assert.IsNull(customer);
\r