private static bool IsOrderDetailInstance(RowEntityType resource)
 {
     return resource.TypeName == CustomRowBasedContext.OrderDetailsFullName;
 }
 private static bool IsRegionInstance(RowEntityType resource)
 {
     return resource.TypeName == CustomRowBasedContext.RegionFullName;
 }
 private static bool IsProductInstance(RowEntityType resource)
 {
     return resource.TypeName == CustomRowBasedContext.ProductFullName;
 }
 private static bool IsCustomerInstance(RowEntityType resource)
 {
     return resource.TypeName == CustomRowBasedContext.CustomerFullName ||
            resource.TypeName == CustomRowBasedContext.CustomerWithBirthdayFullName;
 }
        public object CreateResource(string containerName, string fullTypeName)
        {
            object resource = null;
            if (containerName == "Customers")
            {
                if (fullTypeName == CustomRowBasedContext.CustomerFullName)
                {
                    resource = new RowEntityTypeWithIDAsKey(CustomRowBasedContext.CustomerFullName);
                }

                if (fullTypeName == CustomRowBasedContext.CustomerWithBirthdayFullName)
                {
                    resource = new RowEntityTypeWithIDAsKey(CustomRowBasedContext.CustomerWithBirthdayFullName);
                }
            }
            else if (containerName == "Orders")
            {
                if (fullTypeName == CustomRowBasedContext.OrderFullName)
                {
                    resource = new RowEntityTypeWithIDAsKey(CustomRowBasedContext.OrderFullName);
                }
            }
            else if (containerName == "Regions")
            {
                if (fullTypeName == CustomRowBasedContext.RegionFullName)
                {
                    resource = new RowEntityType(CustomRowBasedContext.RegionFullName);
                }
            }
            else if (fullTypeName == CustomRowBasedContext.AddressFullName)
            {
                // no need to add this to the pending changelist. Only entities need to be added
                return this.ResourceToToken(new RowComplexType(CustomRowBasedContext.AddressFullName));
            }

            if (resource == null)
            {
                throw new Exception(String.Format("Invalid container name '{0}' or type name specified '{1}'", containerName, fullTypeName));
            }
            else
            {
                this.PendingChanges.Add(new KeyValuePair<object, EntityState>(resource, EntityState.Added));
            }

            return this.ResourceToToken(resource);
        }
        internal static void PopulateData()
        {
            int orderCount = 2;
            int customerCount = 3;

            for (int i = 0; i < customerCount; i++)
            {
                RowEntityTypeWithIDAsKey region = new RowEntityTypeWithIDAsKey(CustomRowBasedContext.RegionFullName);
                region.ID = i;
                region.Properties["Name"] = "Region" + i.ToString();
                regions.Add(region);

                RowEntityTypeWithIDAsKey customer = (i % 2 == 0) ? new RowEntityTypeWithIDAsKey(CustomRowBasedContext.CustomerFullName) : new RowEntityTypeWithIDAsKey(CustomRowBasedContext.CustomerWithBirthdayFullName);
                customer.ID = i;
                customer.Properties["Name"] = "Customer " + i.ToString();
                customer.Properties["NameAsHtml"] = "<html><body>" + customer.Properties["Name"] + "</body></html>";
                customer.Properties["BestFriend"] = (i == 0) ? null : customers[i - 1];
                customer.Properties["Region"] = region;

                if (i % 2 != 0)
                {
                    customer.Properties["Birthday"] = DateTime.Today.AddYears(-30);
                }

                customer.Properties.Add("Orders", new List<RowEntityTypeWithIDAsKey>());

                for (int j = 0; j < orderCount; j++)
                {
                    int orderID = i + 100 * j;
                    int productID = i + 100 * j;
                    double orderDollarAmount = Math.Round(20.1 + 10.1 * j, 2);

                    RowEntityTypeWithIDAsKey o = new RowEntityTypeWithIDAsKey(CustomRowBasedContext.OrderFullName) { ID = orderID };
                    o.Properties["DollarAmount"] = orderDollarAmount;
                    o.Properties["OrderDetails"] = new List<RowEntityType>();
                    o.Properties["Customer"] = customer;
                    ((IList<RowEntityTypeWithIDAsKey>)customer.Properties["Orders"]).Add(o);
                    orders.Add(o);

                    RowEntityTypeWithIDAsKey p = new RowEntityTypeWithIDAsKey(CustomRowBasedContext.ProductFullName) { ID = productID };
                    p.Properties["ProductName"] = "Product #" + p.ID.ToString();
                    p.Properties["Discontinued"] = false;
                    p.Properties["OrderDetails"] = new List<RowEntityType>();
                    products.Add(p);

                    RowEntityType od = new RowEntityType(CustomRowBasedContext.OrderDetailsFullName);
                    od.Properties["ProductID"] = p.ID;
                    od.Properties["OrderID"] = o.ID;
                    od.Properties["UnitPrice"] = orderDollarAmount;
                    od.Properties["Quantity"] = (short)1;
                    orderDetails.Add(od);
                    ((IList<RowEntityType>)o.Properties["OrderDetails"]).Add(od);
                    ((IList<RowEntityType>)p.Properties["OrderDetails"]).Add(od);
                }

                customers.Add(customer);

                if (OpenWebDataServiceHelper.EnableBlobServer)
                {
                    DataServiceStreamProvider.Init();
                    using (System.IO.Stream s = System.IO.File.OpenWrite(DataServiceStreamProvider.GetStoragePath(customer)))
                    {
                        byte[] buffer = new byte[] { 1, 2, 3, 4 };
                        s.Write(buffer, 0, 4);
                        s.Close();
                    }
                }
            }
        }