コード例 #1
0
        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);
                    }
                }
            });
        }