src/Misc/EFTools/EntityDesignDatabaseGeneration/MetadataWorkspaceExtensions.cs
/// <summary>
///     Translate an EDM Type to a SQL type, taking into account facets.
///     Note that certain facets were already taken into account when we obtained
///     them from the DbProviderManifest (FixedLength, Unicode)
/// </summary>
/// <param name="property">A property from which to determine the EDM Type.</param>
/// <returns>The corresponding SQL type.</returns>
Translate an EDM Type to a SQL type, taking into account facets.
Note that certain facets were already taken into account when we obtained
them from the DbProviderManifest (FixedLength, Unicode)
Translate an EDM Type to a SQL type taking into account facets . Note that certain facets were already taken into account when we obtained them from the DbProviderManifest ( FixedLength Unicode )
[SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
        public static string ToStoreType(this EdmProperty property)
        {
            var sqlTypeName = String.Empty;
            var storeTypeUsage = property.TypeUsage;
            Debug.Assert(storeTypeUsage != null, "TypeUsage for property: " + property.Name + " is null");
            if (storeTypeUsage != null)
            {
                var edmType = storeTypeUsage.EdmType;
                Debug.Assert(edmType != null, "Edm Type for: " + storeTypeUsage + " is null");
                if (edmType != null)
                {
                    sqlTypeName = storeTypeUsage.EdmType.Name;
                    var primType = storeTypeUsage.EdmType as PrimitiveType;
                    if (primType != null)
                    {
                        Facet maxLengthFacet = null;
                        Facet precisionFacet = null;
                        Facet scaleFacet = null;
                        switch (primType.PrimitiveTypeKind)
                        {
                            case PrimitiveTypeKind.Binary:
                                storeTypeUsage.Facets.TryGetValue(EdmConstants.facetNameMaxLength, false, out maxLengthFacet);
                                Debug.Assert(
                                    maxLengthFacet != null, "MaxLength facet should exist for binary Store Type: " + storeTypeUsage);
                                if (maxLengthFacet != null
                                    && maxLengthFacet.Description.IsConstant == false)
                                {
                                    sqlTypeName = String.Format(CultureInfo.CurrentCulture, "{0}({1})", sqlTypeName, maxLengthFacet.Value);
                                }
                                break;
                            case PrimitiveTypeKind.String:
                                storeTypeUsage.Facets.TryGetValue(EdmConstants.facetNameMaxLength, false, out maxLengthFacet);
                                Debug.Assert(
                                    maxLengthFacet != null, "MaxLength facet should exist for string Store Type: " + storeTypeUsage);
                                if (maxLengthFacet != null
                                    && maxLengthFacet.Description.IsConstant == false)
                                {
                                    sqlTypeName = String.Format(CultureInfo.CurrentCulture, "{0}({1})", sqlTypeName, maxLengthFacet.Value);
                                }
                                break;
                            case PrimitiveTypeKind.Decimal:
                                storeTypeUsage.Facets.TryGetValue(EdmConstants.facetNamePrecision, false, out precisionFacet);
                                storeTypeUsage.Facets.TryGetValue(EdmConstants.facetNameScale, false, out scaleFacet);
                                Debug.Assert(
                                    precisionFacet != null, "Precision facet should exist for decimal Store Type: " + storeTypeUsage);
                                Debug.Assert(scaleFacet != null, "Scale facet should exist for decimal Store Type: " + storeTypeUsage);
                                if (precisionFacet != null
                                    &&
                                    scaleFacet != null
                                    &&
                                    precisionFacet.Description.IsConstant == false)
                                {
                                    sqlTypeName = String.Format(
                                        CultureInfo.CurrentCulture,
                                        "{0}({1},{2})", sqlTypeName, precisionFacet.Value, scaleFacet.Value);
                                }
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
            return sqlTypeName;
        }
[ SuppressMessage ( " " , " " ) ] public static string ToStoreType ( this EdmProperty property ) { var sqlTypeName = String . Empty ; var storeTypeUsage = property . TypeUsage ; Debug . Assert ( storeTypeUsage != null , " " + property . Name + " " ) ; if ( storeTypeUsage != null ) { var edmType = storeTypeUsage . EdmType ; Debug . Assert ( edmType != null , " " + storeTypeUsage + " " ) ; if ( edmType != null ) { sqlTypeName = storeTypeUsage . EdmType . Name ; var primType = storeTypeUsage . EdmType as PrimitiveType ; if ( primType != null ) { Facet maxLengthFacet = null ; Facet precisionFacet = null ; Facet scaleFacet = null ; switch ( primType . PrimitiveTypeKind ) { case PrimitiveTypeKind . Binary : storeTypeUsage . Facets . TryGetValue ( EdmConstants . facetNameMaxLength , false , out maxLengthFacet ) ; Debug . Assert ( maxLengthFacet != null , " " + storeTypeUsage ) ; if ( maxLengthFacet != null && maxLengthFacet . Description . IsConstant == false ) { sqlTypeName = String . Format ( CultureInfo . CurrentCulture , " " , sqlTypeName , maxLengthFacet . Value ) ; } break ; case PrimitiveTypeKind . String : storeTypeUsage . Facets . TryGetValue ( EdmConstants . facetNameMaxLength , false , out maxLengthFacet ) ; Debug . Assert ( maxLengthFacet != null , " " + storeTypeUsage ) ; if ( maxLengthFacet != null && maxLengthFacet . Description . IsConstant == false ) { sqlTypeName = String . Format ( CultureInfo . CurrentCulture , " " , sqlTypeName , maxLengthFacet . Value ) ; } break ; case PrimitiveTypeKind . Decimal : storeTypeUsage . Facets . TryGetValue ( EdmConstants . facetNamePrecision , false , out precisionFacet ) ; storeTypeUsage . Facets . TryGetValue ( EdmConstants . facetNameScale , false , out scaleFacet ) ; Debug . Assert ( precisionFacet != null , " " + storeTypeUsage ) ; Debug . Assert ( scaleFacet != null , " " + storeTypeUsage ) ; if ( precisionFacet != null && scaleFacet != null && precisionFacet . Description . IsConstant == false ) { sqlTypeName = String . Format ( CultureInfo . CurrentCulture , " " , sqlTypeName , precisionFacet . Value , scaleFacet . Value ) ; } break ; default : break ; } } } } return sqlTypeName ; }
Translate an EDM Type to a SQL type, taking into account facets.
Translate an EDM Type to a SQL type taking into account facets .
The corresponding SQL type.
The corresponding SQL type .
EdmProperty
this
property
A property from which to determine the EDM Type.
A property from which to determine the EDM Type .