Ejemplo n.º 1
0
        public void Does_fallback_to_reference_convention_when_alias_is_used()
        {
            using (var db = OpenDbConnection())
            {
                db.DropTable <AliasedCustomer>();
                db.DropTable <AliasedCustomerAddress>();
                db.CreateTable <AliasedCustomerAddress>();
                db.CreateTable <AliasedCustomer>();

                var customer1 = new AliasedCustomer
                {
                    Name        = "Name 1",
                    WorkAddress = new AliasedCustomerAddress
                    {
                        AddressLine1 = "1 Work Road",
                        Country      = "UK",
                    },
                    HomeAddress = new AliasedCustomerAddress
                    {
                        AddressLine1 = "2 Home Street",
                        Country      = "US",
                    }
                };

                var customer2 = new AliasedCustomer
                {
                    Name        = "Name 2",
                    WorkAddress = new AliasedCustomerAddress
                    {
                        AddressLine1 = "3 Work Road",
                        Country      = "AU",
                    },
                    HomeAddress = new AliasedCustomerAddress
                    {
                        AddressLine1 = "4 Home Street",
                        Country      = "NZ",
                    }
                };

                db.Save(customer1, references: true);
                db.Save(customer2, references: true);

                db.Select <AliasedCustomer>().PrintDump();
                db.Select <AliasedCustomerAddress>().PrintDump();

                var dbCustomers = db.LoadSelect <AliasedCustomer>();
                dbCustomers.PrintDump();

                var dbCustomer1 = dbCustomers.First(x => x.Name == "Name 1");

                Assert.That(dbCustomer1.Name, Is.EqualTo("Name 1"));
                Assert.That(dbCustomer1.WorkAddress, Is.Not.Null);
                Assert.That(dbCustomer1.WorkAddress.Country, Is.EqualTo("UK"));
                Assert.That(dbCustomer1.HomeAddress, Is.Not.Null);
                Assert.That(dbCustomer1.HomeAddress.Country, Is.EqualTo("US"));

                var dbCustomer2 = db.LoadSingleById <AliasedCustomer>(customer2.Id);
                Assert.That(dbCustomer2.Name, Is.EqualTo("Name 2"));
                Assert.That(dbCustomer2.WorkAddress, Is.Not.Null);
                Assert.That(dbCustomer2.WorkAddress.Country, Is.EqualTo("AU"));
                Assert.That(dbCustomer2.HomeAddress, Is.Not.Null);
                Assert.That(dbCustomer2.HomeAddress.Country, Is.EqualTo("NZ"));
            }
        }
        public void Does_fallback_to_reference_convention_when_alias_is_used()
        {
            using (var db = OpenDbConnection())
            {
                db.DropTable<AliasedCustomer>();
                db.DropTable<AliasedCustomerAddress>();
                db.CreateTable<AliasedCustomerAddress>();
                db.CreateTable<AliasedCustomer>();

                var customer1 = new AliasedCustomer
                {
                    Name = "Name 1",
                    WorkAddress = new AliasedCustomerAddress
                    {
                        AddressLine1 = "1 Work Road",
                        Country = "UK",
                    },
                    HomeAddress = new AliasedCustomerAddress
                    {
                        AddressLine1 = "2 Home Street",
                        Country = "US",
                    }
                };

                var customer2 = new AliasedCustomer
                {
                    Name = "Name 2",
                    WorkAddress = new AliasedCustomerAddress
                    {
                        AddressLine1 = "3 Work Road",
                        Country = "AU",
                    },
                    HomeAddress = new AliasedCustomerAddress
                    {
                        AddressLine1 = "4 Home Street",
                        Country = "NZ",
                    }
                };

                db.Save(customer1, references: true);
                db.Save(customer2, references: true);

                db.Select<AliasedCustomer>().PrintDump();
                db.Select<AliasedCustomerAddress>().PrintDump();

                var dbCustomers = db.LoadSelect<AliasedCustomer>();
                dbCustomers.PrintDump();

                var dbCustomer1 = dbCustomers.First(x => x.Name == "Name 1");

                Assert.That(dbCustomer1.Name, Is.EqualTo("Name 1"));
                Assert.That(dbCustomer1.WorkAddress, Is.Not.Null);
                Assert.That(dbCustomer1.WorkAddress.Country, Is.EqualTo("UK"));
                Assert.That(dbCustomer1.HomeAddress, Is.Not.Null);
                Assert.That(dbCustomer1.HomeAddress.Country, Is.EqualTo("US"));

                var dbCustomer2 = db.LoadSingleById<AliasedCustomer>(customer2.Id);
                Assert.That(dbCustomer2.Name, Is.EqualTo("Name 2"));
                Assert.That(dbCustomer2.WorkAddress, Is.Not.Null);
                Assert.That(dbCustomer2.WorkAddress.Country, Is.EqualTo("AU"));
                Assert.That(dbCustomer2.HomeAddress, Is.Not.Null);
                Assert.That(dbCustomer2.HomeAddress.Country, Is.EqualTo("NZ"));
            }
        }