コード例 #1
0
 public void TestStoreBrand()
 {
     using (TransactionScope ts = new TransactionScope())
     using (CarsContext context = new CarsContext())
     {
         Brand volvo = new Brand { Name = "Volvo" };
         context.Brands.Add(volvo);
         context.SaveChanges();
     }
 }
コード例 #2
0
public void TestSavingChanges()
{
    using (var ctx = new CarsContext())
    {
        var objCtx = ((IObjectContextAdapter)ctx).ObjectContext;

        bool eventCalled = false;

        objCtx.SavingChanges += (sender, args) => eventCalled = true;

        ctx.SaveChanges();

        Assert.IsTrue(eventCalled);
    }
}
コード例 #3
0
        public void TestStoreCar()
        {
            using (TransactionScope ts = new TransactionScope())
            using (CarsContext context = new CarsContext())
            {
                Brand volvo = new Brand { Name = "Volvo" };
                Car myVolvo = new Car
                {
                    Brand = volvo,
                    RegistrationNumber = "ABC123"
                };

                context.Cars.Add(myVolvo);
                context.SaveChanges();
            }
        }
コード例 #4
0
        public void TestChangeTracking()
        {
            using (TransactionScope ts = new TransactionScope())
            using (CarsContext context = new CarsContext())
            {
                context.Configuration.AutoDetectChangesEnabled = false;

                Debug.WriteLine("Reading people...");
                var people = context.People.ToArray();
                Debug.WriteLine(string.Format("Type of people[0] is {0}",
                    people[0].GetType().ToString()));

                Debug.WriteLine("Updating birth year of first person...");
                people[0].BirthYear = 1965;

                Debug.WriteLine("Saving changes...");
                context.SaveChanges();
            }
        }
コード例 #5
0
        public void TestNavigationAndForeignKeySetNavigationProperty()
        { 
            using (TransactionScope ts = new TransactionScope())
            using (CarsContext context = new CarsContext())
            {
                var brands = context.Brands.Include(b => b.Cars).OrderBy(b => b.BrandId);
                var car = brands.First().Cars.First();

                Debug.WriteLine("The car has BrandId {0} pointing to Brand \"{1}\"",
                    car.BrandId, car.Brand.Name);
                Brand newBrand = brands.Skip(1).First();
                Debug.WriteLine(string.Format("Setting Brand to {0}", newBrand.Name));
                car.Brand = newBrand;
                Debug.WriteLine(string.Format("The car has BrandId {0} pointing to Brand \"{1}\"",
                    car.BrandId, car.Brand.Name));

                Debug.WriteLine("Saving Changes...");
                context.SaveChanges();
                Debug.WriteLine(string.Format("The car has BrandId {0} pointing to Brand \"{1}\"",
                    car.BrandId, car.Brand.Name));
            }
        }
コード例 #6
0
        public void TestChangeTrackingSimpleType()
        {
            using (TransactionScope ts = new TransactionScope())
            using (CarsContext context = new CarsContext())
            {
                var phoneBook = context.PhoneBook.ToList();
                var entry = phoneBook.First();

                entry.PhoneNumber = "4567";

                Debug.WriteLine("Saving changes...");
                context.SaveChanges();
            }
        }
コード例 #7
0
        public void TestNavigationAndForeignKeySetNavigationPropertyWithProxy()
        {
            using (TransactionScope ts = new TransactionScope())
            using (CarsContext context = new CarsContext())
            {
                var genders = context.Genders.Include(g => g.People).OrderBy(g => g.GenderId);
                var person = genders.First().People.First();

                Debug.WriteLine("The person has GenderId {0} pointing to Gender \"{1}\"",
                    person.GenderId, person.Gender.Description);
                Gender newGender = genders.Skip(1).First();
                Debug.WriteLine(string.Format("Setting Gender to {0}", newGender.Description));
                person.Gender = newGender;
                Debug.WriteLine("The person has GenderId {0} pointing to Gender \"{1}\"",
                    person.GenderId, person.Gender.Description);

                Debug.WriteLine("Saving Changes...");
                context.SaveChanges();
                Debug.WriteLine("The person has GenderId {0} pointing to Gender \"{1}\"",
                    person.GenderId, person.Gender.Description);
            }
        }