public void Add(MigrationOperation newOperation) { List <MigrationOperation> operations; if (_allOperations.TryGetValue(newOperation.GetType(), out operations)) { operations.Add(newOperation); } else { _allOperations.Add(newOperation.GetType(), new List <MigrationOperation> { newOperation }); } }
protected virtual void CheckSchema(MigrationOperation operation) { var schema = operation.GetType() .GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty) .Where(p => p.Name.Contains(nameof(AddForeignKeyOperation.Schema), StringComparison.Ordinal)) .Select(p => p.GetValue(operation) as string) .FirstOrDefault(schemaValue => schemaValue != null); if (schema != null) { var name = operation.GetType() .GetProperty(nameof(AddForeignKeyOperation.Name), BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty) ?.GetValue(operation) as string; throw new InvalidOperationException($"A schema \"{schema}\" has been set for an object of type \"{operation.GetType().Name}\"{(string.IsNullOrEmpty(name) ? string.Empty : $" with the name of \"{name}\"")}. MySQL does not support the EF Core concept of schemas. Any schema property of any \"MigrationOperation\" must be null.");
private static void AssertMigrationOperationProperties(MigrationOperation operation, IEnumerable <string> propertyNames) => Debug.Assert( !operation.GetType() .GetProperties(BindingFlags.Instance | BindingFlags.Public) .Select(p => p.Name) .Except(propertyNames.Concat(new[] { "Item", nameof(MigrationOperation.IsDestructiveChange) })) .Any());
private static void SetSchema(MigrationOperation operation, string?schema) { if (operation == null) { throw new ArgumentNullException(nameof(operation)); } switch (operation) { case CreateTableOperation createTable: SetSchema(createTable, schema); break; case RenameTableOperation renameTable: SetSchema(renameTable, schema); break; case AddForeignKeyOperation addForeignKey: SetSchema(addForeignKey, schema); break; default: var opType = operation.GetType(); SetSchema(operation, opType, "Schema", schema); SetSchema(operation, opType, "PrincipalSchema", schema); break; } }
protected virtual void Generate([NotNull] MigrationOperation operation, [NotNull] IndentedStringBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); throw new InvalidOperationException(Strings.UnknownOperation(operation.GetType())); }
private static List <(string sourceName, string targetName)> GetReplaceGroups( MigrationOperation operation, string sourceTableName, string targetTableName) { List <(string sourceName, string targetName)> resList = new List <(string sourceName, string targetName)> { (sourceTableName, targetTableName) }; string name = operation.GetPropertyValue("Name") as string; if (!name.IsNullOrEmpty() && !(operation is ColumnOperation)) { string[] patterns = new string[] { $"^()({sourceTableName})()$", $"^()({sourceTableName})(_.*?)$", $"^(.*?_)({sourceTableName})(_.*?)$", $"^(.*?_)({sourceTableName})()$" }; foreach (var aPattern in patterns) { if (Regex.IsMatch(name, aPattern)) { var newName = new Regex(aPattern).Replace(name, "${1}" + targetTableName + "$3"); resList.Add((name, newName)); break; } } } Func <PropertyInfo, bool> listPropertyWhere = x => x.PropertyType.IsGenericType && x.PropertyType.GetGenericTypeDefinition() == typeof(List <>) && typeof(MigrationOperation).IsAssignableFrom(x.PropertyType.GetGenericArguments()[0]); //其它 operation.GetType().GetProperties() .Where(x => x.Name != "Name" && x.Name != "Table" && x.PropertyType != typeof(object) && (typeof(MigrationOperation).IsAssignableFrom(x.PropertyType) || listPropertyWhere(x)) ) .ToList() .ForEach(aProperty => { var propertyValue = aProperty.GetValue(operation); if (propertyValue is MigrationOperation propertyOperation) { resList.AddRange(GetReplaceGroups(propertyOperation, sourceTableName, targetTableName)); } else if (listPropertyWhere(aProperty)) { foreach (var aValue in (IEnumerable)propertyValue) { resList.AddRange(GetReplaceGroups((MigrationOperation)aValue, sourceTableName, targetTableName)); } } }); return(resList); } }
private static void AssertMigrationOperationProperties(MigrationOperation operation, IEnumerable <string> propertyNames) { if (operation.GetType() .GetProperties(BindingFlags.Instance | BindingFlags.Public) .Select(p => p.Name) .Except( propertyNames.Concat( new[] { "Item", nameof(AlterDatabaseOperation.IsReadOnly), nameof(MigrationOperation.IsDestructiveChange) })) .FirstOrDefault() is string unexpectedProperty) { throw new InvalidOperationException( $"The 'MigrationOperation' of type '{operation.GetType().Name}' contains an unexpected property '{unexpectedProperty}'."); } }
protected override void Generate(MigrationOperation operation, IModel model,MigrationCommandListBuilder builder) { var operationType = operation.GetType(); if (!_generateActions.TryGetValue(operationType, out var generateAction)) { throw new InvalidOperationException(RelationalStrings.UnknownOperation(GetType().ShortDisplayName(), operationType)); } generateAction(this, operation, model, builder); }
private void ReplaceName(MigrationOperation theOperation, string sourceName, string targetName) { string name = theOperation.GetPropertyValue("Name") as string; string tableName = theOperation.GetPropertyValue("Table") as string; if (!tableName.IsNullOrEmpty()) { theOperation.SetPropertyValue("Table", targetName); } if (!name.IsNullOrEmpty() && !(theOperation is ColumnOperation)) { string[] patterns = new string[] { $"^()({sourceName})()$", $"^()({sourceName})(_.*?)$", $"^(.*?_)({sourceName})(_.*?)$", $"^(.*?_)({sourceName})()$" }; foreach (var aPattern in patterns) { if (Regex.IsMatch(name, aPattern)) { var newName = new Regex(aPattern).Replace(name, "${1}" + targetName + "$3"); theOperation.SetPropertyValue("Name", newName); break; } } } Func <PropertyInfo, bool> propertyWhere = x => x.PropertyType.IsGenericType && x.PropertyType.GetGenericTypeDefinition() == typeof(List <>) && typeof(MigrationOperation).IsAssignableFrom(x.PropertyType.GetGenericArguments()[0]); //其它 theOperation.GetType().GetProperties() .Where(x => x.Name != "Name" && x.Name != "Table" && x.PropertyType != typeof(object) && (typeof(MigrationOperation).IsAssignableFrom(x.PropertyType) || propertyWhere(x)) ) .ToList() .ForEach(aProperty => { var propertyValue = aProperty.GetValue(theOperation); if (propertyValue is MigrationOperation propertyOperation) { ReplaceName(propertyOperation, sourceName, targetName); } else if (propertyWhere(aProperty)) { foreach (var aValue in (IEnumerable)propertyValue) { ReplaceName((MigrationOperation)aValue, sourceName, targetName); } } }); }
private void SetSchema(MigrationOperation operation, string schema) { var opertationType = operation.GetType(); var schemaProperty = opertationType.GetProperty("Schema"); if (schemaProperty != null && schemaProperty.CanWrite) { schemaProperty.SetValue(operation, schema); } schemaProperty = opertationType.GetProperty("PrincipalSchema"); if (schemaProperty != null && schemaProperty.CanWrite) { schemaProperty.SetValue(operation, schema); } var properties = opertationType.GetProperties(); foreach (var property in properties) { if (property.CanRead && property.GetIndexParameters().Length == 0) { var propertyValue = property.GetValue(operation); if (propertyValue is IEnumerable <MigrationOperation> items) { foreach (var item in items) { if (item is AddColumnOperation addColumnOperation) { //if (addColumnOperation.Name != "Id") { SetSchema(item, schema); } } else { SetSchema(item, schema); } } } else if (propertyValue is MigrationOperation migrationOperation) { SetSchema(migrationOperation, schema); } } } }
/// <summary> /// 解析SQL语句。 /// </summary> /// <param name="operation">操作实例。</param> /// <param name="builder">字符串构建实例。</param> protected virtual void Generate( MigrationOperation operation, MigrationCommandListBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); var operationType = operation.GetType(); if (!_generateActions.TryGetValue(operationType, out var generateAction)) { throw new InvalidOperationException(string.Format(Resources.UnknownOperation, operationType, GetType().DisplayName(false))); } generateAction(this, operation, builder); }
protected virtual void Generate( [NotNull] MigrationOperation operation, [CanBeNull] IModel model, [NotNull] MigrationCommandListBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); var operationType = operation.GetType(); Action <MigrationsSqlGenerator, MigrationOperation, IModel, MigrationCommandListBuilder> generateAction; if (!_generateActions.TryGetValue(operationType, out generateAction)) { throw new InvalidOperationException(RelationalStrings.UnknownOperation(GetType().ShortDisplayName(), operationType)); } generateAction(this, operation, model, builder); }
protected override void Generate(MigrationOperation migrationOperation) { if (migrationOperation != null) { Type type = migrationOperation.GetType(); if (typeof(CreateXmlIndexOperation).IsAssignableFrom(type)) { using (var writer = Writer()) { ((CreateXmlIndexOperation)migrationOperation).WriteUpAsSql(writer); Statement(writer); } return; } } base.Generate(migrationOperation); }
protected override void Generate(MigrationOperation migrationOperation) { if (migrationOperation != null) { Type type = migrationOperation.GetType(); if (typeof(CreateXmlIndexOperation).IsAssignableFrom(type)) { using (var writer = Writer()) { ((CreateXmlIndexOperation)migrationOperation).WriteUpAsSql(writer, "Microsoft.EntityFrameworkCore.SqlServer"); Statement(writer); } return; } else if (typeof(RemoveXmlIndexOperation).IsAssignableFrom(type)) { using (var writer = Writer()) { ((RemoveXmlIndexOperation)migrationOperation).WriteUpAsSql(writer, "Microsoft.EntityFrameworkCore.SqlServer"); Statement(writer); } return; } else if (typeof(RenameIndexOperation).IsAssignableFrom(type)) { using (var writer = Writer()) { string newName = ((RenameIndexOperation)migrationOperation).NewName; string name = ((RenameIndexOperation)migrationOperation).Name; string table = ((RenameIndexOperation)migrationOperation).Table; writer.WriteLine($"sp_rename '{table}.{name}', '{newName}', 'INDEX';"); Statement(writer); return; } } } base.Generate(migrationOperation); }
protected override void Generate([NotNull] MigrationOperation operation, [NotNull] IndentedStringBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); var asCreateExtensionOperation = operation as NpgsqlCreatePostgresExtensionOperation; if (asCreateExtensionOperation != null) { Generate(asCreateExtensionOperation, builder); return; } var asDropExtensionOperation = operation as NpgsqlDropPostgresExtensionOperation; if (asDropExtensionOperation != null) { Generate(asDropExtensionOperation, builder); return; } throw new InvalidOperationException(DesignCoreStrings.UnknownOperation(operation.GetType())); }
protected IEnumerable <MigrationStatement> Generate(MigrationOperation operation) { throw new NotSupportedException(string.Format("Unknown operation '{0}'.", operation.GetType().FullName)); }
public virtual void Generate( [NotNull] MigrationOperation operation, [CanBeNull] IModel model, [NotNull] SqlBatchBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); throw new InvalidOperationException(Strings.UnknownOperation(GetType().Name, operation.GetType().Name)); }
protected override void VisitDefault(MigrationOperation operation, IndentedStringBuilder builder) { builder.Append(operation.GetType().Name).Append("Sql"); }
/// <summary> /// Generates SQL for a <see cref="MigrationOperation" />. /// Allows derived providers to handle additional operation types. /// Generated SQL should be added using the Statement method. /// </summary> /// <param name="migrationOperation"> The operation to produce SQL for. </param> protected virtual void Generate(MigrationOperation migrationOperation) { Check.NotNull(migrationOperation, "migrationOperation"); throw Error.SqlServerMigrationSqlGenerator_UnknownOperation(GetType().Name, migrationOperation.GetType().FullName); }