コード例 #1
0
        public void LinqToSqlInsert01()
        {
            Northwind db = CreateDB();

            Customer cust = db.Customers.FirstOrDefault(c => c.CustomerID == "MCSFT");
            if (cust != null)
            {
                try
                {
                    db.Customers.DeleteOnSubmit(cust);
                    db.SubmitChanges();
                }
                catch
                {
                    Assert.Ignore("Inconclusive: the object already exist. And the couldn't be removed");
                }
            }

            var q = from c in db.Customers
                    where c.Region == "WA"
                    select c;

            var newCustomer = new Customer
            {
                CustomerID = "MCSFT",
                CompanyName = "Microsoft",
                ContactName = "John Doe",
                ContactTitle = "Sales Manager",
                Address = "1 Microsoft Way",
                City = "Redmond",
                Region = "WA",
                PostalCode = "98052",
                Country = "USA",
                Phone = "(425) 555-1234",
                Fax = null
            };

            db.Customers.InsertOnSubmit(newCustomer);
            db.SubmitChanges();

            var reloadedCustomer = db.Customers.First(c => c.CustomerID == newCustomer.CustomerID);

            Assert.AreEqual(reloadedCustomer.CompanyName, newCustomer.CompanyName);
            Assert.AreEqual(reloadedCustomer.ContactName, newCustomer.ContactName);
            Assert.AreEqual(reloadedCustomer.ContactTitle, newCustomer.ContactTitle);
            Assert.AreEqual(reloadedCustomer.Address, newCustomer.Address);
            Assert.AreEqual(reloadedCustomer.City, newCustomer.City);
            Assert.AreEqual(reloadedCustomer.Region, newCustomer.Region);
            Assert.AreEqual(reloadedCustomer.PostalCode, newCustomer.PostalCode);
            Assert.AreEqual(reloadedCustomer.Country, newCustomer.Country);
            Assert.AreEqual(reloadedCustomer.Phone, newCustomer.Phone);
            Assert.AreEqual(reloadedCustomer.Fax, newCustomer.Fax);

            db.Customers.DeleteOnSubmit(reloadedCustomer);
            db.SubmitChanges();
        }
コード例 #2
0
ファイル: Table.cs プロジェクト: sushihangover/playscript
        public void AttachAll()
        {
            var db = CreateDB();
            var customers = new Customer[] { new Customer { CustomerID = "ID1" }, new Customer { CustomerID = "ID2" } };
            db.Customers.AttachAll(customers);

            Assert.IsFalse(customers.Any(c => db.Customers.Contains(c)));

        }
コード例 #3
0
ファイル: Table.cs プロジェクト: sushihangover/playscript
        public void Attach06()
        {
            var db = CreateDB();
            var customer = new Customer();
            //http://geekswithblogs.net/michelotti/archive/2007/12/17/117791.aspx
            //we have to do a test related with that stuff, but we need to change all of datacontexts

            Assert.Ignore();
        }
コード例 #4
0
ファイル: Table.cs プロジェクト: sushihangover/playscript
 public void Attach05()
 {
     var db = CreateDB();
     var customer = new Customer();
     db.Customers.Attach(customer, true);
 }
コード例 #5
0
ファイル: Table.cs プロジェクト: sushihangover/playscript
        public void Attach04()
        {
            var db = CreateDB();
            var originalCustomer = db.Customers.First();
            var customer = new Customer();
            db.Customers.Attach(customer, originalCustomer);

            AssertHelper.Greater(db.Customers.GetModifiedMembers(customer).Count(), 0);
        }
コード例 #6
0
ファイル: Table.cs プロジェクト: sushihangover/playscript
 public void Attach03()
 {
     var db = CreateDB();
     db.ObjectTrackingEnabled = false;
     var customer = new Customer();
     db.Customers.Attach(customer);
 }
コード例 #7
0
ファイル: LinqToSqlSamples.cs プロジェクト: jetlive/skiaming
        public void LinqToSqlInsert10()
        {
            // 通常,通过从其他层反序列化 XML 来获取要附加的实体。
            // 不支持将实体从一个 DataContext 附加到另一个 DataContext。
            // 因此若要复制反序列化实体的操作,将在此处重新创建这些实体。
            Customer c1;
            List<Order> deserializedOrders = new List<Order>();
            Customer deserializedC1;

            using (Northwind tempdb = new Northwind(connString))
            {
                c1 = tempdb.Customers.Single(c => c.CustomerID == "ALFKI");
                Console.WriteLine("Customer {0}'s original address {1}", c1.CustomerID, c1.Address);
                Console.WriteLine();
                deserializedC1 = new Customer { Address = c1.Address, City = c1.City,
                                                CompanyName=c1.CompanyName, ContactName=c1.ContactName,
                                                ContactTitle=c1.ContactTitle, Country=c1.Country,
                                                CustomerID=c1.CustomerID, Fax=c1.Fax,
                                                Phone=c1.Phone, PostalCode=c1.PostalCode,
                                                Region=c1.Region};
                Customer tempcust = tempdb.Customers.Single(c => c.CustomerID == "ANTON");
                foreach (Order o in tempcust.Orders)
                {
                    Console.WriteLine("Order {0} belongs to customer {1}", o.OrderID, o.CustomerID);
                    deserializedOrders.Add(new Order {CustomerID=o.CustomerID, EmployeeID=o.EmployeeID,
                                                      Freight=o.Freight, OrderDate=o.OrderDate, OrderID=o.OrderID,
                                                      RequiredDate=o.RequiredDate, ShipAddress=o.ShipAddress,
                                                      ShipCity=o.ShipCity, ShipName=o.ShipName,
                                                      ShipCountry=o.ShipCountry, ShippedDate=o.ShippedDate,
                                                      ShipPostalCode=o.ShipPostalCode, ShipRegion=o.ShipRegion,
                                                      ShipVia=o.ShipVia});
                }

                Console.WriteLine();

                Customer tempcust2 = tempdb.Customers.Single(c => c.CustomerID == "CHOPS");
                var c3Orders = tempcust2.Orders.ToList();
                foreach (Order o in c3Orders)
                {
                    Console.WriteLine("Order {0} belongs to customer {1}", o.OrderID, o.CustomerID);
                }
                Console.WriteLine();
            }

            using (Northwind db2 = new Northwind(connString))
            {
                // 将第一个实体附加到当前数据上下文,以跟踪更改。
                db2.Customers.Attach(deserializedC1);
                Console.WriteLine("***** Update Customer ALFKI's address ******");
                Console.WriteLine();
                // 更改所跟踪的实体。
                deserializedC1.Address = "123 First Ave";

                // 附加订单列表中的所有实体。
                db2.Orders.AttachAll(deserializedOrders);
                // 将订单更新为属于其他客户。
                Console.WriteLine("****** Assign all Orders belong to ANTON to CHOPS ******");
                Console.WriteLine();

                foreach (Order o in deserializedOrders)
                {
                    o.CustomerID = "CHOPS";
                }

                // 在当前数据上下文中提交更改。
                db2.SubmitChanges();
            }

            // 检查是否像预期的那样提交了订单。
            using (Northwind db3 = new Northwind(connString))
            {
                Customer dbC1 = db3.Customers.Single(c => c.CustomerID == "ALFKI");

                Console.WriteLine("Customer {0}'s new address {1}", dbC1.CustomerID, dbC1.Address);
                Console.WriteLine();

                Customer dbC2 = db3.Customers.Single(c => c.CustomerID == "CHOPS");

                foreach (Order o in dbC2.Orders)
                {
                    Console.WriteLine("Order {0} belongs to customer {1}", o.OrderID, o.CustomerID);
                }

            }

            CleanupInsert10();
        }
コード例 #8
0
partial         void UpdateCustomer(Customer instance);
コード例 #9
0
        public void LinqToSqlInsert10()
        {
           
            // Typically you would get entities to attach from deserializing XML from another tier.
            // It is not supported to attach entities from one DataContext to another DataContext.  
            // So to duplicate deserializing the entities, the entities will be recreated here.
            Customer c1;
            List<Order> deserializedOrders = new List<Order>();
            Customer deserializedC1;

            using (Northwind tempdb = new Northwind(connString))
            {
                c1 = tempdb.Customers.Single(c => c.CustomerID == "ALFKI");
                Console.WriteLine("Customer {0}'s original address {1}", c1.CustomerID, c1.Address);
                Console.WriteLine();
                deserializedC1 = new Customer { Address = c1.Address, City = c1.City,
                                                CompanyName=c1.CompanyName, ContactName=c1.ContactName,
                                                ContactTitle=c1.ContactTitle, Country=c1.Country,
                                                CustomerID=c1.CustomerID, Fax=c1.Fax,
                                                Phone=c1.Phone, PostalCode=c1.PostalCode,
                                                Region=c1.Region};
                Customer tempcust = tempdb.Customers.Single(c => c.CustomerID == "ANTON");
                foreach (Order o in tempcust.Orders)
                {
                    Console.WriteLine("Order {0} belongs to customer {1}", o.OrderID, o.CustomerID);
                    deserializedOrders.Add(new Order {CustomerID=o.CustomerID, EmployeeID=o.EmployeeID,
                                                      Freight=o.Freight, OrderDate=o.OrderDate, OrderID=o.OrderID,
                                                      RequiredDate=o.RequiredDate, ShipAddress=o.ShipAddress,
                                                      ShipCity=o.ShipCity, ShipName=o.ShipName,
                                                      ShipCountry=o.ShipCountry, ShippedDate=o.ShippedDate,
                                                      ShipPostalCode=o.ShipPostalCode, ShipRegion=o.ShipRegion,
                                                      ShipVia=o.ShipVia});
                }
                
                Console.WriteLine();

                Customer tempcust2 = tempdb.Customers.Single(c => c.CustomerID == "CHOPS");
                var c3Orders = tempcust2.Orders.ToList();
                foreach (Order o in c3Orders)
                {
                    Console.WriteLine("Order {0} belongs to customer {1}", o.OrderID, o.CustomerID);
                }
                Console.WriteLine();
            }

            using (Northwind db2 = new Northwind(connString))
            {
                // Attach the first entity to the current data context, to track changes.
                db2.Customers.Attach(deserializedC1);
                Console.WriteLine("***** Update Customer ALFKI's address ******");
                Console.WriteLine();
                // Change the entity that is tracked.
                deserializedC1.Address = "123 First Ave";

                // Attach all entities in the orders list.
                db2.Orders.AttachAll(deserializedOrders);
                // Update the orders to belong to another customer.
                Console.WriteLine("****** Assign all Orders belong to ANTON to CHOPS ******");
                Console.WriteLine();

                foreach (Order o in deserializedOrders)
                {
                    o.CustomerID = "CHOPS";
                }

                // Submit the changes in the current data context.
                db2.SubmitChanges();
            }

            // Check that the orders were submitted as expected.
            using (Northwind db3 = new Northwind(connString))
            {
                Customer dbC1 = db3.Customers.Single(c => c.CustomerID == "ALFKI");

                Console.WriteLine("Customer {0}'s new address {1}", dbC1.CustomerID, dbC1.Address);
                Console.WriteLine();

                Customer dbC2 = db3.Customers.Single(c => c.CustomerID == "CHOPS");

                foreach (Order o in dbC2.Orders)
                {
                    Console.WriteLine("Order {0} belongs to customer {1}", o.OrderID, o.CustomerID);
                }
              
            }

            CleanupInsert10();
        }
コード例 #10
0
        public void LinqToSqlInsert01() {
            var q =
                from c in db.Customers
                where c.Region == "WA"
                select c;

            Console.WriteLine("*** BEFORE ***");
            ObjectDumper.Write(q);


            Console.WriteLine();
            Console.WriteLine("*** INSERT ***");
            var newCustomer = new Customer { CustomerID = "MCSFT",
                                             CompanyName = "Microsoft",
                                             ContactName = "John Doe",
                                             ContactTitle = "Sales Manager",
                                             Address = "1 Microsoft Way",
                                             City = "Redmond",
                                             Region = "WA",
                                             PostalCode = "98052",
                                             Country = "USA",
                                             Phone = "(425) 555-1234",
                                             Fax = null
                                           };
            db.Customers.InsertOnSubmit(newCustomer);
            db.SubmitChanges();


            Console.WriteLine();
            Console.WriteLine("*** AFTER ***");
            ObjectDumper.Write(q);



            Cleanup64();  // Restore previous database state
        }
コード例 #11
0
ファイル: WriteTest.cs プロジェクト: nlhepler/mono
        public void G20_CustomerCacheHitComparingToLocalVariable()
        {
             Northwind db = CreateDB();
             try
             {
                Customer c1 = new Customer() { CustomerID = "temp", CompanyName = "Test", ContactName = "Test" };
                db.Customers.InsertOnSubmit(c1);
                db.SubmitChanges();

                string id = "temp";
                var res = from c in db.Customers
                          where c.CustomerID == id
                          select c;

                Assert.AreEqual(1, res.Count(), "#1");

                db.ExecuteCommand("DELETE FROM \"Customers\" WHERE \"CustomerID\"='temp'");

                res = from c in db.Customers
                      where c.CustomerID == id
                      select c;
                Assert.AreEqual(0, res.Count(), "#2");
            }
            finally
            {
                db.ExecuteCommand("DELETE FROM \"Customers\" WHERE \"CustomerID\"='temp'");
            }
        }
コード例 #12
0
ファイル: WriteTest.cs プロジェクト: nlhepler/mono
        public void G16_CustomerCacheHit()
        {
            Northwind db = CreateDB();
            Customer c1 = new Customer() { CustomerID = "temp", CompanyName = "Test", ContactName = "Test" };
            db.Customers.InsertOnSubmit(c1);
            db.SubmitChanges();
            db.ExecuteCommand("delete from \"Customers\" WHERE \"CustomerID\"='temp'");

            var res = db.Customers.First(c => c.CustomerID == "temp");
            Assert.IsNotNull(res);
        }
コード例 #13
0
ファイル: WriteTest.cs プロジェクト: nlhepler/mono
        public void G7_InsertTableWithStringPK()
        {
            Northwind db = CreateDB();
            db.ExecuteCommand("DELETE FROM [Customers] WHERE [CustomerID]='TEMP_'");

            Customer custTemp = new Customer
            {
                CustomerID = "TEMP_",
                CompanyName = "Magellan",
                ContactName = "Antonio Pigafetta",
                City = "Lisboa",
            };
            db.Customers.InsertOnSubmit(custTemp);
            db.SubmitChanges();
        }
コード例 #14
0
ファイル: WriteTest.cs プロジェクト: nlhepler/mono
        public void G6_UpdateTableWithStringPK()
        {
            Northwind db = CreateDB();
            var customer = new Customer
            {
                CompanyName = "Test Company",
                ContactName = "Test Customer",
                CustomerID  = "BT___",
            };
            db.Customers.InsertOnSubmit(customer);
            db.SubmitChanges();
            Customer BT = db.Customers.Single(c => c.CustomerID == "BT___");
            BT.Country = "U.K.";
            db.SubmitChanges();

            db.Customers.DeleteOnSubmit(customer);
            db.SubmitChanges();
        }
コード例 #15
0
partial         void DeleteCustomer(Customer instance);
コード例 #16
0
partial         void InsertCustomer(Customer instance);
コード例 #17
0
 public void SimpleMemberAccess01()
 {
     var customer = new Customer();
     var orders = customer.Orders;
 }
コード例 #18
0
ファイル: Table.cs プロジェクト: sushihangover/playscript
        public void Attach02()
        {
            var db = CreateDB();
            var customer = new Customer();
            db.Customers.Attach(customer);

            Assert.IsFalse(db.Customers.Contains(customer));
            var db2 = CreateDB();
            db2.Customers.Attach(customer);
        }
コード例 #19
0
ファイル: Transactions.cs プロジェクト: nlhepler/mono
        public void TransactionCheckAndRollbackInsert()
        {
            Northwind db = CreateDB();
            DbTransaction t = BeginTransaction(db);

            var cust = new Customer();
            int beforeCustomersCount = db.Customers.Count();

            string id = new object().GetHashCode().ToString().Substring(0, 5);
            cust.CustomerID = id;
            cust.Country = "Spain";
            cust.CompanyName = "Coco";

            db.Customers.InsertOnSubmit(cust);
            db.SubmitChanges();

            int afterCustomercount = db.Customers.Count();
            Assert.IsTrue(beforeCustomersCount + 1 == afterCustomercount);

            t.Rollback();

            afterCustomercount = db.Customers.Count();
            Assert.IsTrue(beforeCustomersCount == afterCustomercount);

            // The Count is correct.  However, DataContext doesn't know that the 
            // transaction was aborted, and will satisfy the following from
            // an internal cache
            var customer = db.Customers.FirstOrDefault(c => c.CustomerID == id);
            Assert.IsNotNull(customer);

            // Let's let DataContext know that it doesn't exist anymore.
            db.Customers.DeleteOnSubmit(customer);
            db.SubmitChanges(); // Note no exception from deleting a non-existent entity

            customer = db.Customers.FirstOrDefault(c => c.CustomerID == id);
            Assert.IsNull(customer);
        }