为什么SQL Profiler中没有BEGIN TRANSACTION语句

时间:2012-10-31 09:01:55

标签: c# sql-server transactions entity-framework-4.1 dbcontext

我有两个POCO对象,Customer和CustomerAddress,如下所示。

public partial class Customer
    {
        public Customer()
        {
            this.CustomerAddresses = new HashSet<CustomerAddress>();
            this.Orders = new HashSet<Order>();
        }

        public int ID { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public string MiddleName { get; set; }
        public System.DateTime DateOfBirth { get; set; }
        public byte Gender { get; set; }

        public virtual ICollection<CustomerAddress> CustomerAddresses { get; set; }
        public virtual ICollection<Order> Orders { get; set; }
    }

public partial class CustomerAddress
    {
        public int ID { get; set; }
        public int CustomerID { get; set; }
        public byte AddressType { get; set; }
        public string Street { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Country { get; set; }

        public virtual Customer Customer { get; set; }
    }

我正在创建一个新的Customer对象,如下所示。

Customer cust = new Customer();
            cust.ID = 9;
            cust.LastName = "Ross";
            cust.FirstName = "Taylor";
            cust.DateOfBirth = DateTime.Now;
            cust.Gender = 0;

            CustomerAddress custAdd = new CustomerAddress();
            custAdd.ID = 9;
            custAdd.Customer = cust;
            custAdd.AddressType = 0;
            custAdd.Street = "1 Main Street";
            custAdd.City = "Princeton";
            custAdd.State = "CA";
            custAdd.Country = "USA";

            DbConnection MyContext = new DbConnection(); ;
            MyContext.Customers.Add(cust);
            MyContext.CustomerAddresses.Add(custAdd);
            MyContext.SaveChanges();

我启动了SQL Profiler。保存此数据后,将针对数据库运行两个插入脚本,一个用于Customer,另一个用于CustomerAddress。但我的困惑是我在SQL Profiler中看不到任何BEGIN TRANSACTION和COMMIT语句。如果由于某种原因,CustomerAddress的插入脚本失败,我希望整个事务失败,我不确定如果没有BEGIN TRANSACTION我是否会得到它。我在这里做错了吗?

0 个答案:

没有答案