Exemplo n.º 1
0
 private IEnumerable <string> ModelColumnNames()
 {
     return(_modelType.GetProperties().Where(pi =>
                                             !pi.HasAttribute <CalculatedAttribute>() &&
                                             !pi.HasAttribute <NotMappedAttribute>() &&
                                             CreateTable.IsSupportedType(pi.PropertyType)).Select(pi => pi.SqlColumnName()));
 }
Exemplo n.º 2
0
        private IEnumerable <Action> GetNewColumns(IEnumerable <Type> modelTypes, IDbConnection connection)
        {
            List <Action> results = new List <Action>();

            var schemaColumns = GetSchemaColumns(connection);

            var dbObjects = schemaColumns.GroupBy(item => new DbObject(item.Schema, item.TableName)
            {
                ObjectID = item.ObjectID
            });
            var dcObjectIDs = dbObjects.ToDictionary(obj => obj.Key, obj => obj.Key.ObjectID);

            Dictionary <DbObject, Type> dcModelTypes = new Dictionary <DbObject, Type>();

            var modelColumns = modelTypes.SelectMany(mt => mt.GetProperties()
                                                     .Where(pi => CreateTable.IsSupportedType(pi.PropertyType) && !pi.HasAttribute <NotMappedAttribute>())
                                                     .Select(pi =>
            {
                DbObject obj = DbObject.FromType(mt);
                if (!dcModelTypes.ContainsKey(obj))
                {
                    dcModelTypes.Add(obj, mt);
                }
                return(new ColumnRef()
                {
                    Schema = obj.Schema,
                    TableName = obj.Name,
                    ColumnName = pi.SqlColumnName(),
                    PropertyInfo = pi
                });
            }));

            var newColumns = modelColumns.Where(mcol =>
                                                !_createdTables.Contains(new DbObject(mcol.Schema, mcol.TableName)) &&
                                                !schemaColumns.Any(scol => mcol.Equals(scol)));

            foreach (var colGroup in newColumns.GroupBy(item => new DbObject(item.Schema, item.TableName)))
            {
                if (!_deletedTables.Contains(colGroup.Key))
                {
                    _deletedTables.Add(colGroup.Key);
                }
                if (!_createdTables.Contains(colGroup.Key))
                {
                    _createdTables.Add(colGroup.Key);
                }

                if (IsTableEmpty(connection, colGroup.Key.Schema, colGroup.Key.Name) || dcModelTypes[colGroup.Key].HasAttribute <AllowDropAttribute>())
                {
                    colGroup.Key.ObjectID = dcObjectIDs[colGroup.Key];
                    results.Add(new DropTable(colGroup.Key, connection));
                    results.Add(new CreateTable(dcModelTypes[colGroup.Key]));
                }
                else
                {
                    results.Add(new AddColumns(dcModelTypes[colGroup.Key], colGroup, connection));
                }
            }

            return(results);
        }