public override bool AddRemark(EntityInfo entity) { return(true); }
public virtual void ExistLogic(EntityInfo entityInfo) { if (entityInfo.Columns.HasValue()) { //Check.Exception(entityInfo.Columns.Where(it => it.IsPrimarykey).Count() > 1, "Multiple primary keys do not support modifications"); var tableName = GetTableName(entityInfo); var dbColumns = this.Context.DbMaintenance.GetColumnInfosByTableName(tableName); ConvertColumns(dbColumns); var entityColumns = entityInfo.Columns.Where(it => it.IsIgnore == false).ToList(); var dropColumns = dbColumns .Where(dc => !entityColumns.Any(ec => dc.DbColumnName.Equals(ec.OldDbColumnName, StringComparison.CurrentCultureIgnoreCase))) .Where(dc => !entityColumns.Any(ec => dc.DbColumnName.Equals(ec.DbColumnName, StringComparison.CurrentCultureIgnoreCase))) .ToList(); var addColumns = entityColumns .Where(ec => ec.OldDbColumnName.IsNullOrEmpty() || !dbColumns.Any(dc => dc.DbColumnName.Equals(ec.OldDbColumnName, StringComparison.CurrentCultureIgnoreCase))) .Where(ec => !dbColumns.Any(dc => ec.DbColumnName.Equals(dc.DbColumnName, StringComparison.CurrentCultureIgnoreCase))).ToList(); var alterColumns = entityColumns .Where(ec => !dbColumns.Any(dc => dc.DbColumnName.Equals(ec.OldDbColumnName, StringComparison.CurrentCultureIgnoreCase))) .Where(ec => dbColumns.Any(dc => dc.DbColumnName.Equals(ec.DbColumnName) && ((ec.Length != dc.Length && !UtilMethods.GetUnderType(ec.PropertyInfo).IsEnum() && UtilMethods.GetUnderType(ec.PropertyInfo).IsIn(UtilConstants.StringType)) || ec.IsNullable != dc.IsNullable || IsSamgeType(ec, dc)))).ToList(); var renameColumns = entityColumns .Where(it => !string.IsNullOrEmpty(it.OldDbColumnName)) .Where(entityColumn => dbColumns.Any(dbColumn => entityColumn.OldDbColumnName.Equals(dbColumn.DbColumnName, StringComparison.CurrentCultureIgnoreCase))) .ToList(); var isMultiplePrimaryKey = dbColumns.Where(it => it.IsPrimarykey).Count() > 1 || entityColumns.Where(it => it.IsPrimarykey).Count() > 1; var isChange = false; foreach (var item in addColumns) { this.Context.DbMaintenance.AddColumn(tableName, EntityColumnToDbColumn(entityInfo, tableName, item)); isChange = true; } foreach (var item in dropColumns) { this.Context.DbMaintenance.DropColumn(tableName, item.DbColumnName); isChange = true; } foreach (var item in alterColumns) { this.Context.DbMaintenance.UpdateColumn(tableName, EntityColumnToDbColumn(entityInfo, tableName, item)); isChange = true; } foreach (var item in renameColumns) { this.Context.DbMaintenance.RenameColumn(tableName, item.OldDbColumnName, item.DbColumnName); isChange = true; } foreach (var item in entityColumns) { var dbColumn = dbColumns.FirstOrDefault(dc => dc.DbColumnName.Equals(item.DbColumnName, StringComparison.CurrentCultureIgnoreCase)); if (dbColumn == null) { continue; } bool pkDiff, idEntityDiff; KeyAction(item, dbColumn, out pkDiff, out idEntityDiff); if (dbColumn != null && pkDiff && !idEntityDiff && isMultiplePrimaryKey == false) { var isAdd = item.IsPrimarykey; if (isAdd) { this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName); } else { this.Context.DbMaintenance.DropConstraint(tableName, string.Format("PK_{0}_{1}", tableName, item.DbColumnName)); } } else if ((pkDiff || idEntityDiff) && isMultiplePrimaryKey == false) { ChangeKey(entityInfo, tableName, item); } } if (isMultiplePrimaryKey) { var oldPkNames = dbColumns.Where(it => it.IsPrimarykey).Select(it => it.DbColumnName.ToLower()).OrderBy(it => it).ToList(); var newPkNames = entityColumns.Where(it => it.IsPrimarykey).Select(it => it.DbColumnName.ToLower()).OrderBy(it => it).ToList(); if (!Enumerable.SequenceEqual(oldPkNames, newPkNames)) { Check.Exception(true, ErrorMessage.GetThrowMessage("Modification of multiple primary key tables is not supported. Delete tables while creating", "不支持修改多主键表,请删除表在创建")); } } if (isChange && IsBackupTable) { this.Context.DbMaintenance.BackupTable(tableName, tableName + DateTime.Now.ToString("yyyyMMddHHmmss"), MaxBackupDataRows); } } }
protected virtual string GetTableName(EntityInfo entityInfo) { return(this.Context.EntityMaintenance.GetTableName(entityInfo.EntityName)); }
private void SetColumns(EntityInfo result) { foreach (var property in result.Type.GetProperties()) { EntityColumnInfo column = new EntityColumnInfo(); //var isVirtual = property.GetGetMethod().IsVirtual; //if (isVirtual) continue; var sugarColumn = property.GetCustomAttributes(typeof(SugarColumn), true) .Where(it => it is SugarColumn) .Select(it => (SugarColumn)it) .FirstOrDefault(); column.DbTableName = result.DbTableName; column.EntityName = result.EntityName; column.PropertyName = property.Name; column.PropertyInfo = property; if (sugarColumn.IsNullOrEmpty()) { column.DbColumnName = property.Name; } else { if (sugarColumn.IsIgnore == false) { column.DbColumnName = sugarColumn.ColumnName.IsNullOrEmpty() ? property.Name : sugarColumn.ColumnName; column.IsPrimarykey = sugarColumn.IsPrimaryKey; column.IsIdentity = sugarColumn.IsIdentity; column.ColumnDescription = sugarColumn.ColumnDescription; column.IsNullable = sugarColumn.IsNullable; column.Length = sugarColumn.Length; column.OldDbColumnName = sugarColumn.OldColumnName; column.DataType = sugarColumn.ColumnDataType; column.DecimalDigits = sugarColumn.DecimalDigits; column.OracleSequenceName = sugarColumn.OracleSequenceName; column.IsOnlyIgnoreInsert = sugarColumn.IsOnlyIgnoreInsert; column.IsEnableUpdateVersionValidation = sugarColumn.IsEnableUpdateVersionValidation; column.IsTranscoding = sugarColumn.IsTranscoding; column.SerializeDateTimeFormat = sugarColumn.SerializeDateTimeFormat; column.IsJson = sugarColumn.IsJson; column.NoSerialize = sugarColumn.NoSerialize; column.DefaultValue = sugarColumn.DefaultValue; column.IndexGroupNameList = sugarColumn.IndexGroupNameList; column.IsOnlyIgnoreUpdate = sugarColumn.IsOnlyIgnoreUpdate; } else { column.IsIgnore = true; column.NoSerialize = sugarColumn.NoSerialize; } } if (this.Context.MappingColumns.HasValue()) { var golbalMappingInfo = this.Context.MappingColumns.FirstOrDefault(it => it.EntityName.Equals(result.EntityName, StringComparison.CurrentCultureIgnoreCase) && it.PropertyName == column.PropertyName); if (golbalMappingInfo != null) { column.DbColumnName = golbalMappingInfo.DbColumnName; } } if (this.Context.IgnoreColumns.HasValue()) { var golbalMappingInfo = this.Context.IgnoreColumns.FirstOrDefault(it => it.EntityName.Equals(result.EntityName, StringComparison.CurrentCultureIgnoreCase) && it.PropertyName == column.PropertyName); if (golbalMappingInfo != null) { column.IsIgnore = true; } } if (this.Context.CurrentConnectionConfig.ConfigureExternalServices != null && this.Context.CurrentConnectionConfig.ConfigureExternalServices.EntityService != null) { this.Context.CurrentConnectionConfig.ConfigureExternalServices.EntityService(property, column); } result.Columns.Add(column); } }