private bool InferKeyColumns(LoadMethodSessionState session, IList<TableDetailsRow> columns, List<string> pKeys, DbObjectKey tableKey, ref ICollection<string> primaryKeys)
        {
            for (int i = 0; i < columns.Count; i++)
            {
                if (!columns[i].IsNullable)
                {
                    PrimitiveType primitiveType;
                    bool _;
                    if (session.TryGetStorePrimitiveType(columns[i].DataType, out primitiveType, out _) &&
                        MetadataUtil.IsValidKeyType(_targetEntityFrameworkVersion, primitiveType))
                    {
                        pKeys.Add(columns[i].ColumnName);
                    }
                }
            }

            // if there are valid key column candidates, make them the new key columns
            if (pKeys.Count > 0)
            {
                primaryKeys = pKeys;
            }
            else
            {
                primaryKeys = null;
            }

            return primaryKeys != null;
        }
 private TypeUsage GetScalarFunctionTypeUsage(LoadMethodSessionState session, string dataType, out bool excludedForTarget)
 {
     PrimitiveType primitiveType;
     if (session.TryGetStorePrimitiveType(dataType, out primitiveType, out excludedForTarget))
     {
         TypeUsage usage = TypeUsage.Create(primitiveType, FacetValues.NullFacetValues);
         return usage;
     }
     return null;
 }
        private IList<EdmProperty> CreateEdmProperties(
            LoadMethodSessionState session,
            IList<TableDetailsRow> columns,
            DbObjectKey tableKey,
            List<EdmSchemaError> errors,
            out IList<string> excludedColumns)
        {
            Debug.Assert(columns.Count != 0, "columns.Count != 0");
            Debug.Assert(errors != null, "errors != null");

            var members = new List<EdmProperty>();
            excludedColumns = new List<string>();
            foreach (TableDetailsRow row in columns)
            {
                PrimitiveType primitiveType;
                bool excludedForTarget = false;
                if (row.IsDataTypeNull() || !session.TryGetStorePrimitiveType(row.DataType, out primitiveType, out excludedForTarget))
                {
                    string message;
                    if (!row.IsDataTypeNull())
                    {
                        message = excludedForTarget ?
                            Strings.UnsupportedDataTypeForTarget(row.DataType, row.GetMostQualifiedTableName(), row.ColumnName) :
                            Strings.UnsupportedDataType(row.DataType, row.GetMostQualifiedTableName(), row.ColumnName);
                    }
                    else
                    {
                        message = Strings.UnsupportedDataTypeUnknownType(row.ColumnName, row.GetMostQualifiedTableName());
                    }

                    errors.Add(new EdmSchemaError(message, (int)ModelBuilderErrorCode.UnsupportedType, EdmSchemaErrorSeverity.Warning));
                    excludedColumns.Add(row.ColumnName);

                    continue;
                }

                Dictionary<string, Facet> facets = primitiveType.GetAssociatedFacetDescriptions().ToDictionary(fd => fd.FacetName, fd => fd.DefaultValueFacet);
                facets[DbProviderManifest.NullableFacetName] = Facet.Create(facets[DbProviderManifest.NullableFacetName].Description, row.IsNullable);

                if (primitiveType.PrimitiveTypeKind == PrimitiveTypeKind.Decimal)
                {
                    Facet precision;
                    if (facets.TryGetValue(DbProviderManifest.PrecisionFacetName, out precision))
                    {
                        if (!row.IsPrecisionNull() && !precision.Description.IsConstant)
                        {
                            if (row.Precision < precision.Description.MinValue || row.Precision > precision.Description.MaxValue)
                            {
                                DbObjectKey key = row.CreateDbObjectKey(tableKey.ObjectType);
                                errors.Add(new EdmSchemaError(
                                    Strings.ColumnFacetValueOutOfRange(
                                        DbProviderManifest.PrecisionFacetName,
                                        row.Precision,
                                        precision.Description.MinValue,
                                        precision.Description.MaxValue,
                                        row.ColumnName,
                                        key),
                                    (int)ModelBuilderErrorCode.FacetValueOutOfRange,
                                    EdmSchemaErrorSeverity.Warning));
                                excludedColumns.Add(row.ColumnName);
                                continue;
                            }
                            facets[precision.Name] = Facet.Create(precision.Description, (byte)row.Precision);
                        }
                    }
                    Facet scale;
                    if (facets.TryGetValue(DbProviderManifest.ScaleFacetName, out scale))
                    {
                        if (!row.IsScaleNull() && !scale.Description.IsConstant)
                        {
                            if (row.Scale < scale.Description.MinValue || row.Scale > scale.Description.MaxValue)
                            {
                                DbObjectKey key = row.CreateDbObjectKey(tableKey.ObjectType);
                                errors.Add(new EdmSchemaError(
                                    Strings.ColumnFacetValueOutOfRange(
                                        DbProviderManifest.ScaleFacetName,
                                        row.Scale,
                                        scale.Description.MinValue,
                                        scale.Description.MaxValue,
                                        row.ColumnName,
                                        key),
                                    (int)ModelBuilderErrorCode.FacetValueOutOfRange,
                                    EdmSchemaErrorSeverity.Warning));
                                excludedColumns.Add(row.ColumnName);
                                continue;
                            }
                            facets[scale.Name] = Facet.Create(scale.Description, (byte)row.Scale);
                        }
                    }
                }
                else if (primitiveType.PrimitiveTypeKind == PrimitiveTypeKind.DateTime || 
                         primitiveType.PrimitiveTypeKind == PrimitiveTypeKind.Time ||
                         primitiveType.PrimitiveTypeKind == PrimitiveTypeKind.DateTimeOffset)
                {
                    Facet datetimePrecision;
                    if (facets.TryGetValue(DbProviderManifest.PrecisionFacetName, out datetimePrecision))
                    {
                        if (!row.IsDateTimePrecisionNull() && !datetimePrecision.Description.IsConstant)
                        {
                            if (row.DateTimePrecision < datetimePrecision.Description.MinValue || row.DateTimePrecision > datetimePrecision.Description.MaxValue)
                            {
                                DbObjectKey key = row.CreateDbObjectKey(tableKey.ObjectType);
                                errors.Add(new EdmSchemaError(
                                    Strings.ColumnFacetValueOutOfRange(
                                        DbProviderManifest.PrecisionFacetName,
                                        row.DateTimePrecision,
                                        datetimePrecision.Description.MinValue,
                                        datetimePrecision.Description.MaxValue,
                                        row.ColumnName,
                                        key),
                                    (int)ModelBuilderErrorCode.FacetValueOutOfRange,
                                    EdmSchemaErrorSeverity.Warning));
                                excludedColumns.Add(row.ColumnName);
                                continue;
                            }
                            facets[datetimePrecision.Name] = Facet.Create(datetimePrecision.Description, (byte)row.DateTimePrecision);
                        }
                    }
                }
                else if (primitiveType.PrimitiveTypeKind == PrimitiveTypeKind.String ||
                         primitiveType.PrimitiveTypeKind == PrimitiveTypeKind.Binary)
                {
                    Facet maxLength;
                    if (facets.TryGetValue(DbProviderManifest.MaxLengthFacetName, out maxLength))
                    {
                        if (!row.IsMaximumLengthNull() && !maxLength.Description.IsConstant)
                        {
                            if (row.MaximumLength < maxLength.Description.MinValue || row.MaximumLength > maxLength.Description.MaxValue)
                            {
                                DbObjectKey key = row.CreateDbObjectKey(tableKey.ObjectType);
                                errors.Add(new EdmSchemaError(
                                    Strings.ColumnFacetValueOutOfRange(
                                        DbProviderManifest.MaxLengthFacetName,
                                        row.MaximumLength,
                                        maxLength.Description.MinValue,
                                        maxLength.Description.MaxValue,
                                        row.ColumnName,
                                        key),
                                    (int)ModelBuilderErrorCode.FacetValueOutOfRange,
                                    EdmSchemaErrorSeverity.Warning));
                                excludedColumns.Add(row.ColumnName);
                                continue;
                            }
                            facets[maxLength.Name] = Facet.Create(maxLength.Description, row.MaximumLength);
                        }
                    }
                }

                if (!row.IsIsIdentityNull() && row.IsIdentity)
                {
                    Facet facet = Facet.Create(System.Data.Metadata.Edm.Converter.StoreGeneratedPatternFacet, StoreGeneratedPattern.Identity);
                    facets.Add(facet.Name, facet);
                }
                else if (!row.IsIsServerGeneratedNull() && row.IsServerGenerated)
                {
                    Facet facet = Facet.Create(System.Data.Metadata.Edm.Converter.StoreGeneratedPatternFacet, StoreGeneratedPattern.Computed);
                    facets.Add(facet.Name, facet);
                }

                members.Add(new EdmProperty(row.ColumnName, TypeUsage.Create(primitiveType, facets.Values)));
            }

            return members;
        }