예제 #1
0
        private static void ImportSingle(IEntitySet entitySetToCreateIn, string[] headers, string[] data)
        {
            // Create the new entity
            IEntityObject newEntity = entitySetToCreateIn.AddNew();

            Microsoft.LightSwitch.Details.IEntityProperty currentProperty;
            object newValue;

            // Loop through all propertyNames from the first line of the file
            for (int i = 0; i < headers.Count() ; i++)
            {
                try
                {
                    // Get the property from the new entity by name
                    currentProperty = newEntity.Details.Properties[headers[i]];
                }
                catch (ArgumentException)
                {

                    throw new InvalidOperationException(String.Format("A property named {0} does not exist on the entity named {1}.", headers[i], newEntity.Details.Name));
                }

                try
                {
                    // Convert the value
                    newValue = Convert.ChangeType(data[i], currentProperty.PropertyType, null);

                    currentProperty.Value = newValue;
                }
                catch (System.FormatException)
                {
                    throw new InvalidOperationException(String.Format("The following line has an invalid value for property {0}.  Aborting the import.\nData: {1}", headers[i], String.Join(",", data)));
                }
            }
        }