Exemple #1
0
        static void Main(string[] args)
        {
            using (var context = new OwnedEntityContext())
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();

                context.Add(new Order
                {
                    ShippingAddress = new StreetAddress()
                    {
                        City     = null,
                        PostCode = 12345
                    }
                });

                // Why does this works when City i required?
                context.SaveChanges();
            }

            using (var context = new OwnedEntityContext())
            {
                var orders = context.Orders.Include(a => a.ShippingAddress).ToList();

                foreach (var order in orders)
                {
                    // One order is printed here, but ShippingAddress is null even if we should get a value for PostCode.
                    Console.WriteLine($"OrderID: {order.Id} PostCode: {order.ShippingAddress?.PostCode} City: {order?.ShippingAddress?.City}");
                }
            }
        }
        static void Main(string[] args)
        {
            using (var context = new OwnedEntityContext())
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();

                context.Add(new DetailedOrder
                {
                    Status       = OrderStatus.Pending,
                    OrderDetails = new OrderDetails
                    {
                        ShippingAddress = new StreetAddress {
                            City = "London", Street = "221 B Baker St"
                        },
                        BillingAddress = new StreetAddress {
                            City = "New York", Street = "11 Wall Street"
                        }
                    }
                });

                context.SaveChanges();
            }

            using (var context = new OwnedEntityContext())
            {
                #region DetailedOrderQuery
                var order = context.DetailedOrders.First(o => o.Status == OrderStatus.Pending);
                Console.WriteLine($"First pending order will ship to: {order.OrderDetails.ShippingAddress.City}");
                #endregion
            }
        }