コード例 #1
0
        public void Execute()
        {
            string customerId;
            string orderId;

            using (var t = new TransactionScope())
            {
                using (var session = Program.Store.OpenSession())
                {
                    var customer = new Customer {FirstName = "Erik"};
                    session.Store(customer);
                    customerId = customer.Id;

                    session.SaveChanges();
                }

                //throw new Exception();

                using (var session = Program.Store.OpenSession())
                {
                    var order = new Order {CustomerId = customerId, Created = DateTime.Now};
                    session.Store(order);
                    orderId = order.Id;

                    session.SaveChanges();

                    t.Complete();
                }
            }

            Console.WriteLine("Customer {0} and order {1} saved!", customerId, orderId);
        }
コード例 #2
0
        public ActionResult Create(Customer customer)
        {
            if (!ModelState.IsValid)
            {
                return View(customer);
            }

            _session.Store(customer);
            _session.SaveChanges();

            return RedirectToAction("Index");
        }
コード例 #3
0
        public void Execute()
        {
            string id;
            using (var session = Program.Store.OpenSession())
            {
                var customer = new Customer
                                   {
                                       FirstName = "Erik",
                                       LastName = "Juhlin",
                                       Email = "*****@*****.**",
                                       AcceptNewsletter = true,
                                       Addresses = new Collection<Address>
                                                       {
                                                           new Address
                                                               {
                                                                   Street = "Helmfeltsgatan 7",
                                                                   City = "Helsingborg"
                                                               }
                                                       }
                                   };
                session.Store(customer);
                session.Store(new Customer
                                  {
                                      FirstName = "Mattias",
                                      LastName = "Jonsson",
                                      AcceptNewsletter = true,
                                      Addresses = new Collection<Address>
                                                      {
                                                          new Address
                                                              {
                                                                  Street = "Tågagatan",
                                                                  City = "Helsingborg"
                                                              }
                                                      }
                                  });
                id = customer.Id;
                Console.WriteLine(id);
                session.SaveChanges();
            }

            using (var session = Program.Store.OpenSession())
            {
                var customer = session.Load<Customer>(id);
                Console.WriteLine(customer.FirstName + " " + customer.Addresses.First().City);
            }
        }