Esempio n. 1
0
        public ContextDefinition ReadContext()
        {
            var contextDef = new ContextDefinition();

            _context.Model.GetEntityTypes().ToList().ForEach(entity =>
            {
                var newEntity = new EntityDefinition {
                    EntityName = entity.Name, EntityClrType = entity.ClrType
                };

                // Get its DbSet
                var allDbSets = _context.GetType().GetProperties().Where(p => p.PropertyType.FullName.Contains("DbSet")).ToList();
                var dbSet     = allDbSets.SingleOrDefault(p => p.PropertyType.GenericTypeArguments.Length == 1 && p.PropertyType.GenericTypeArguments[0].FullName == entity.Name);
                if (dbSet != null)
                {
                    newEntity.DbSetName = dbSet.Name;
                }

                var tableName      = entity.ClrType.Name;
                var allAnnotations = entity.GetAnnotations();
                var tableAttribute = entity.ClrType.GetTypeInfo().GetCustomAttribute <TableAttribute>(true);
                if (tableAttribute != null)
                {
                    tableName = tableAttribute.Name;
                }
                else if (newEntity.DbSetName != null)
                {
                    tableName = newEntity.DbSetName;
                }

                newEntity.FieldMapping = HandleFieldMapping(entity);

                contextDef.AllTables.Add(tableName, newEntity);
            });

            return(contextDef);
        }
Esempio n. 2
0
        private List <ReconcileStep> ReadFile(string filePath, object dbSetToAddTo, EntityDefinition entityDetails)
        {
            var steps = new List <ReconcileStep>();

            using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
            {
                using (var reader = ExcelReaderFactory.CreateReader(stream, new ExcelReaderConfiguration()
                {
                    FallbackEncoding = Encoding.GetEncoding("utf-8")
                }))
                {
                    string[] headers     = ParseIndexes(reader);
                    var      currentLine = 1;
                    while (reader.Read())
                    {
                        ++currentLine;
                        var entityType = entityDetails.EntityClrType;
                        var newObject  = Activator.CreateInstance(entityType);
                        var mapping    = entityDetails.FieldMapping;
                        for (var col = 0; col < headers.Length; ++col)
                        {
                            if (!mapping.ContainsKey(headers[col]))
                            {
                                throw new EffortException($"Field not found in entity mapping for entity {entityDetails.EntityName}: {headers[col]}");
                            }
                            var mappedField = mapping[headers[col]];

                            if (mappedField.IsDependantAndShadowKey)
                            {
                                var newStep = new ReconcileStep
                                {
                                    TargetObject     = newObject,
                                    TargetProperty   = mappedField.Name,
                                    PrincipalClrType = mappedField.PrincipalEntityClrType
                                };
                                FillPropertyFromParsedField(reader, newStep, col, newStep.GetType().GetProperty("PrincipalId"));
                                steps.Add(newStep);
                            }
                            else
                            {
                                var targetProperty = entityType.GetProperty(mappedField.Name);

                                try
                                {
                                    FillPropertyFromParsedField(reader, newObject, col, targetProperty);
                                }
                                catch (Exception e)
                                {
                                    throw new EffortException($"Error occured when trying to convert line {currentLine}, column {col + 1} of file {filePath}: " + e.Message + "\n" + e.StackTrace);
                                }
                            }
                        }
                        if (dbSetToAddTo != null)
                        {
                            dbSetToAddTo.GetType().GetMethod("Add").Invoke(dbSetToAddTo, new[] { newObject });
                        }
                        else
                        {
                            var addMethod = _context.GetType().GetMethods().Where(m => m.Name == "Add" && m.IsGenericMethod == false).FirstOrDefault();
                            addMethod.Invoke(_context, new[] { newObject });
                        }
                    }
                }
            }

            return(steps);
        }