/// <summary> /// Returns the sql primitive/native type name. /// It will include size, precision or scale depending on type information present in the /// type facets /// </summary> /// <param name="type"></param> /// <returns></returns> private string GetSqlPrimitiveType(TypeUsage type) { var primitiveType = type.GetEdmType <PrimitiveType>(); string typeName = primitiveType.Name; bool isFixedLength = false; int maxLength = 0; string length = "max"; byte decimalPrecision = 0; byte decimalScale = 0; switch (primitiveType.PrimitiveTypeKind) { case PrimitiveTypeKind.Binary: //maxLength = type.GetFacetValueOrDefault<int>(FacetInfo.MaxLengthFacetName, FacetInfo.BinaryMaxMaxLength); //if (maxLength == FacetInfo.BinaryMaxMaxLength) { // length = "max"; //} //else { // length = maxLength.ToString(CultureInfo.InvariantCulture); //} //isFixedLength = type.GetFacetValueOrDefault<bool>(FacetInfo.FixedLengthFacetName, false); //typeName = (isFixedLength ? "binary(" : "varbinary(") + length + ")"; typeName = "w"; break; case PrimitiveTypeKind.Guid: typeName = "c(38)"; break; case PrimitiveTypeKind.String: isFixedLength = type.GetFacetValueOrDefault <bool>(FacetInfo.FixedLengthFacetName, false); maxLength = type.GetFacetValueOrDefault <int>(FacetInfo.MaxLengthFacetName, Int32.MinValue); if (maxLength == Int32.MinValue) { length = "254"; } else { length = maxLength.ToString(CultureInfo.InvariantCulture); } if (isFixedLength) { typeName = "c(" + length + ")"; } else { typeName = "v(" + length + ")"; } //// Using the Unicode Facet to determine if the field is binary... //var binary = type.GetFacetValueOrDefault<bool>(FacetInfo.UnicodeFacetName, false); //if (binary) { // typeName += " NOCPTRANS"; //} break; case PrimitiveTypeKind.DateTime: typeName = "t"; break; case PrimitiveTypeKind.Time: typeName = "time"; break; //case PrimitiveTypeKind.DateTimeOffset: // typeName = "datetimeoffset"; // break; case PrimitiveTypeKind.Single: decimalPrecision = type.GetFacetValueOrDefault <byte>(FacetInfo.PrecisionFacetName, 18); Debug.Assert(decimalPrecision > 0, "decimal precision must be greater than zero"); decimalScale = type.GetFacetValueOrDefault <byte>(FacetInfo.ScaleFacetName, 0); Debug.Assert(decimalPrecision >= decimalScale, "decimalPrecision must be greater or equal to decimalScale"); Debug.Assert(decimalPrecision <= 38, "decimalPrecision must be less than or equal to 38"); typeName = "f(" + decimalPrecision + "," + decimalScale + ")"; break; case PrimitiveTypeKind.Double: decimalPrecision = type.GetFacetValueOrDefault <byte>(FacetInfo.PrecisionFacetName, 18); Debug.Assert(decimalPrecision > 0, "decimal precision must be greater than zero"); typeName = "b"; break; case PrimitiveTypeKind.Decimal: decimalPrecision = type.GetFacetValueOrDefault <byte>(FacetInfo.PrecisionFacetName, 18); Debug.Assert(decimalPrecision > 0, "decimal precision must be greater than zero"); decimalScale = type.GetFacetValueOrDefault <byte>(FacetInfo.ScaleFacetName, 0); Debug.Assert(decimalPrecision >= decimalScale, "decimalPrecision must be greater or equal to decimalScale"); Debug.Assert(decimalPrecision <= 38, "decimalPrecision must be less than or equal to 38"); typeName = "n(" + decimalPrecision + "," + decimalScale + ")"; break; case PrimitiveTypeKind.Int64: typeName = "n(20)"; break; case PrimitiveTypeKind.Byte: case PrimitiveTypeKind.Int16: case PrimitiveTypeKind.Int32: typeName = "i"; break; case PrimitiveTypeKind.Boolean: typeName = "l"; break; //case PrimitiveTypeKind.Guid: // typeName = "uniqueidentifier"; // break; default: throw new NotSupportedException("Unsupported EdmType: " + primitiveType.PrimitiveTypeKind); } return(typeName); }