/// <summary> /// 设置实体 /// </summary> /// <param name="includeComments"></param> /// <param name="includeExtendedPropertyComments"></param> private void SetupEntity(bool includeComments, ExtendedPropertyCommentsStyle includeExtendedPropertyComments) { string comments; if (includeComments) { comments = " // " + Name; if (IsPrimaryKey) { if (IsPrimaryKeyViaUniqueIndex) { comments += " (Primary key via unique index " + UniqueIndexName + ")"; } else { comments += " (Primary key)"; } } } else { comments = string.Empty; } if (includeExtendedPropertyComments == ExtendedPropertyCommentsStyle.AtEndOfField && !string.IsNullOrEmpty(ExtendedProperty)) { if (string.IsNullOrEmpty(comments)) { comments = " // " + ExtendedProperty; } else { comments += ". " + ExtendedProperty; } } if (IsPrimaryKey) { StringBuilder build = new StringBuilder(); build.AppendLine(string.Format("private {0} {1} _{2};", PropertyType, CodeFirstTools.CheckNullable(this), PropertyNameHumanCase.ToLower())); build.AppendLine(string.Format("public {0} {1} {2} {3} {4}", PropertyType, CodeFirstTools.CheckNullable(this), PropertyName, "{ get { return _" + PropertyNameHumanCase.ToLower() + " ; } set { this.Id = value; this._" + PropertyNameHumanCase.ToLower() + " = value; }}", comments)); Entity = build.ToString(); } else { Entity = string.Format("public {0}{1} {2} {3}{4}", PropertyType, CodeFirstTools.CheckNullable(this), PropertyName, IsStoreGenerated ? "{ get; internal set; }" : "{ get; set; }", comments); } }
private static Column CreateColumn(IDataRecord rdr, Regex rxClean, Table table, bool useCamelCase, Regex columnFilterExclude, Func <Column, Table, Column> updateColumn) { try { if (rdr == null) { throw new ArgumentNullException("rdr"); } string typename = rdr["TYPENAME"].ToString().Trim().ToLower(); var scale = decimal.ToInt32(decimal.Parse(rdr["SCALE"].ToString())); var precision = decimal.ToInt32(decimal.Parse(rdr["PRECISION"].ToString())); if (typename.ToLower() == "number" && scale == 0 && precision == 0) { scale = int.MinValue; precision = 38; } var col = new Column { Name = rdr["COLUMNNAME"].ToString().Trim(), TypeName = typename, PropertyType = GetPropertyType(typename, ref scale, precision), MaxLength = decimal.ToInt32(decimal.Parse(rdr["MAXLENGTH"].ToString())), Precision = precision, Default = rdr["Default"].ToString().Trim(), DateTimePrecision = decimal.ToInt32(decimal.Parse(rdr["DATETIMEPRECISION"].ToString())), Scale = scale, Ordinal = decimal.ToInt32(decimal.Parse(rdr["ORDINAL"].ToString())), IsIdentity = rdr["ISIDENTITY"].ToString().Trim().ToLower() == "1", IsNullable = rdr["ISNULLABLE"].ToString().Trim().ToLower() == "1", IsStoreGenerated = rdr["ISSTOREGENERATED"].ToString().Trim().ToLower() == "1", IsPrimaryKey = rdr["PRIMARYKEY"].ToString().Trim().ToLower() == "1", PrimaryKeyOrdinal = decimal.ToInt32(decimal.Parse(rdr["PRIMARYKEYORDINAL"].ToString())), IsForeignKey = rdr["ISFOREIGNKEY"].ToString().Trim().ToLower() == "1" }; Assembly asm = Assembly.Load("EntityFramework"); Version v = new Version("5.0.0.0"); if (typename.ToLower() == "float" && asm.GetName().Version > v) { if (precision == 126) { col.Precision = 38; } else if (precision == 63) { col.Precision = 19; } } if (columnFilterExclude != null && !col.IsPrimaryKey && columnFilterExclude.IsMatch(col.Name)) { col.Hidden = true; } col.IsFixedLength = (typename.ToLower() == "char" || typename.ToLower() == "nchar"); col.IsUnicode = !(typename.ToLower() == "char" || typename.ToLower() == "varchar2" || typename.ToLower() == "clob" || typename.ToLower() == "long"); col.IsRowVersion = col.IsStoreGenerated && !col.IsNullable && typename == "timestamp"; if (col.IsRowVersion) { col.MaxLength = 8; } col.CleanUpDefault(); col.PropertyName = CodeFirstTools.CleanUp(col.Name); col.PropertyName = rxClean.Replace(col.PropertyName, "_$1"); // Make sure property name doesn't clash with class name if (col.PropertyName == table.NameHumanCase) { col.PropertyName = col.PropertyName + "_"; } col.PropertyNameHumanCase = (useCamelCase ? Inflector.ToTitleCase(col.PropertyName) : col.PropertyName).Replace(" ", ""); if (col.PropertyNameHumanCase == string.Empty) { col.PropertyNameHumanCase = col.PropertyName; } // Make sure property name doesn't clash with class name if (col.PropertyNameHumanCase == table.NameHumanCase) { col.PropertyNameHumanCase = col.PropertyNameHumanCase + "_"; } if (char.IsDigit(col.PropertyNameHumanCase[0])) { col.PropertyNameHumanCase = "_" + col.PropertyNameHumanCase; } if (CodeFirstTools.CheckNullable(col) != string.Empty) { table.HasNullableColumns = true; } col = updateColumn(col, table); // If PropertyType is empty, return null. Most likely ignoring a column due to legacy (such as OData not supporting spatial types) if (string.IsNullOrEmpty(col.PropertyType)) { return(null); } return(col); } catch (Exception ee) { throw ee; } }
private static Column CreateColumn(IDataRecord rdr, Regex rxClean, Table table, bool useCamelCase, Regex columnFilterExclude, Func <Column, Table, Column> updateColumn) { if (rdr == null) { throw new ArgumentNullException("rdr"); } string typename = rdr["TypeName"].ToString().Trim().ToLower(); var scale = (int)rdr["Scale"]; var precision = (int)rdr["Precision"]; var col = new Column { Name = rdr["ColumnName"].ToString().Trim(), TypeName = typename, PropertyType = GetPropertyType(typename, scale, precision), MaxLength = (int)rdr["MaxLength"], Precision = precision, Default = rdr["Default"].ToString().Trim(), DateTimePrecision = (int)rdr["DateTimePrecision"], Scale = scale, Ordinal = (int)rdr["Ordinal"], IsIdentity = rdr["IsIdentity"].ToString().Trim().ToLower() == "true", IsNullable = rdr["IsNullable"].ToString().Trim().ToLower() == "true", IsStoreGenerated = rdr["IsStoreGenerated"].ToString().Trim().ToLower() == "true", IsPrimaryKey = rdr["PrimaryKey"].ToString().Trim().ToLower() == "true", PrimaryKeyOrdinal = (int)rdr["PrimaryKeyOrdinal"], IsForeignKey = rdr["IsForeignKey"].ToString().Trim().ToLower() == "true" }; if (columnFilterExclude != null && !col.IsPrimaryKey && columnFilterExclude.IsMatch(col.Name)) { col.Hidden = true; } col.IsFixedLength = (typename == "char" || typename == "nchar"); col.IsUnicode = !(typename == "char" || typename == "varchar" || typename == "text"); col.IsRowVersion = col.IsStoreGenerated && !col.IsNullable && typename == "timestamp"; if (col.IsRowVersion) { col.MaxLength = 8; } col.CleanUpDefault(); col.PropertyName = CodeFirstTools.CleanUp(col.Name); col.PropertyName = rxClean.Replace(col.PropertyName, "_$1"); // Make sure property name doesn't clash with class name if (col.PropertyName == table.NameHumanCase) { col.PropertyName = col.PropertyName + "_"; } col.PropertyNameHumanCase = (useCamelCase ? Inflector.ToTitleCase(col.PropertyName) : col.PropertyName).Replace(" ", ""); if (col.PropertyNameHumanCase == string.Empty) { col.PropertyNameHumanCase = col.PropertyName; } // Make sure property name doesn't clash with class name if (col.PropertyNameHumanCase == table.NameHumanCase) { col.PropertyNameHumanCase = col.PropertyNameHumanCase + "_"; } if (char.IsDigit(col.PropertyNameHumanCase[0])) { col.PropertyNameHumanCase = "_" + col.PropertyNameHumanCase; } if (CodeFirstTools.CheckNullable(col) != string.Empty) { table.HasNullableColumns = true; } col = updateColumn(col, table); // If PropertyType is empty, return null. Most likely ignoring a column due to legacy (such as OData not supporting spatial types) if (string.IsNullOrEmpty(col.PropertyType)) { return(null); } return(col); }