Пример #1
0
        private static IEdmPrimitiveTypeReference GetPrimitiveTypeReference(
            IProperty efProperty)
        {
            var kind = EdmPrimitiveTypeKind.None;
            var propertyType = TypeHelper.GetUnderlyingTypeOrSelf(efProperty.ClrType);

            if (!primitiveTypeKindMap.TryGetValue(propertyType, out kind))
            {
                return null;
            }

            if (TypeHelper.IsDateTime(propertyType))
            {
                RelationalPropertyAnnotations annotations = new RelationalPropertyAnnotations(efProperty, null);
                var columnType = annotations.ColumnType;

                if (string.Equals(columnType, "date", StringComparison.OrdinalIgnoreCase))
                {
                    kind = EdmPrimitiveTypeKind.Date;
                }
            }
            else if (TypeHelper.IsTimeSpan(propertyType))
            {
                RelationalPropertyAnnotations annotations = new RelationalPropertyAnnotations(efProperty, null);
                var columnType = annotations.ColumnType;

                if (string.Equals(columnType, "time", StringComparison.OrdinalIgnoreCase))
                {
                    kind = EdmPrimitiveTypeKind.TimeOfDay;
                }
            }

            switch (kind)
            {
                default:
                    return EdmCoreModel.Instance.GetPrimitive(kind, efProperty.IsNullable);
                case EdmPrimitiveTypeKind.Binary:
                    return EdmCoreModel.Instance.GetBinary(
                        efProperty.GetMaxLength() < 0,
                        efProperty.GetMaxLength(),
                        efProperty.IsNullable);
                case EdmPrimitiveTypeKind.Decimal:

                    // TODO GitHubIssue#57: Complete EF7 to EDM model mapping
                    ////return EdmCoreModel.Instance.GetDecimal(
                    ////    efProperty.Precision, efProperty.Scale,
                    ////    efProperty.Nullable);
                    return EdmCoreModel.Instance.GetDecimal(efProperty.IsNullable);
                case EdmPrimitiveTypeKind.String:
                    // TODO GitHubIssue#57: Complete EF7 to EDM model mapping
                    return EdmCoreModel.Instance.GetString(
                        efProperty.GetMaxLength() < 0,
                        efProperty.GetMaxLength(),
                        null,
                        efProperty.IsNullable);
                case EdmPrimitiveTypeKind.DateTimeOffset:
                case EdmPrimitiveTypeKind.Duration:

                    // TODO GitHubIssue#57: Complete EF7 to EDM model mapping
                    ////return EdmCoreModel.Instance.GetTemporal(
                    ////    kind, efProperty.Precision, efProperty.Nullable);
                    return EdmCoreModel.Instance.GetTemporal(kind, efProperty.IsNullable);
            }
        }
Пример #2
0
        private static IEdmEntityType CreateEntityType(
            IModel efModel,
            IEntityType efEntityType,
            EdmModel model,
            out List<EdmStructuralProperty> concurrencyProperties)
        {
            // TODO GitHubIssue#36 : support complex and entity inheritance
            var entityType = new EdmEntityType(
                efEntityType.ClrType.Namespace, efEntityType.ClrType.Name);
            concurrencyProperties = null;
            foreach (var efProperty in efEntityType.GetProperties())
            {
                var type = ModelProducer.GetPrimitiveTypeReference(efProperty);
                if (type != null)
                {
                    string defaultValue = null;
                    RelationalPropertyAnnotations annotations = new RelationalPropertyAnnotations(efProperty, null);

                    if (annotations.DefaultValue != null)
                    {
                        defaultValue = annotations.DefaultValue.ToString();
                    }

                    var property = entityType.AddStructuralProperty(
                        efProperty.Name,
                        type,
                        defaultValue,
                        EdmConcurrencyMode.None); // alway None:replaced by OptimisticConcurrency annotation

                    // TODO GitHubIssue#57: Complete EF7 to EDM model mapping
                    if (efProperty.StoreGeneratedAlways)
                    {
                        SetComputedAnnotation(model, property);
                    }

                    if (efProperty.IsConcurrencyToken)
                    {
                        concurrencyProperties = concurrencyProperties ?? new List<EdmStructuralProperty>();
                        concurrencyProperties.Add(property);
                    }
                }
            }

            var key = efEntityType.GetPrimaryKey();
            if (key != null)
            {
                entityType.AddKeys(key.Properties
                    .Select(p => entityType.FindProperty(p.Name))
                    .Cast<IEdmStructuralProperty>());
            }

            return entityType;
        }