示例#1
0
        public ModelComplexType BuildComplexTypeNavigationProperty(EdmxModel edmxModel, ModelSchema modelSchema, CsdlEntityTypeModel entityTypeModel, MetaModelBuilder metaModelBuilder, ModelErrors errors)
        {
            ModelComplexType result     = null;
            var entityTypeModelName     = entityTypeModel.Name;
            var entityTypeModelFullName = entityTypeModel.FullName;

            var lstFound = modelSchema.ComplexTypes.FindByKey2(entityTypeModelFullName ?? entityTypeModelName);

            if (lstFound.Count == 1)
            {
                result = lstFound[0];
            }
            else if (lstFound.Count > 1)
            {
                errors.AddErrorOrThrow(new ModelErrorInfo($"found key {entityTypeModelFullName ?? entityTypeModelName} #{lstFound.Count} times.", entityTypeModelFullName ?? entityTypeModelName));
                result = lstFound[0];
            }
            else
            {
                result = BuildComplexType(edmxModel, modelSchema, entityTypeModel, metaModelBuilder, errors);
            }
            if (result == null)
            {
                return(null);
            }

            foreach (var navigationProperty in entityTypeModel.NavigationProperty)
            {
                if (navigationProperty.RelationshipName is null)
                {
                    //navigationProperty.Name
                    //navigationProperty.TypeModel
                    //navigationProperty.PartnerModel
                    throw new NotImplementedException("v4 NavigationProperty");
                }
                else
                {
                    // v3
                    //if (navigationProperty.FromRoleModel is null) {
                    //} else {
                    //}

                    if (navigationProperty.ToRoleModel is null)
                    {
                    }
                    else
                    {
                        var toModel           = navigationProperty.ToRoleModel.TypeModel;
                        var lstToComplexTypes = modelSchema.ComplexTypes.FindByKey2(toModel.FullName);
                        if (lstToComplexTypes.Count == 1)
                        {
                            var toComplexType = lstToComplexTypes[0];
                            var navigationPropertyExternalName = navigationProperty.Name;
#warning magic needed here
                            var navigationPropertyName = navigationPropertyExternalName;

                            bool isCollection = false;
                            bool isOptional   = false;
                            switch (navigationProperty.ToRoleModel.GetMultiplicity())
                            {
                            case MultiplicityKind.Unknown:
                                break;

                            case MultiplicityKind.OneOptional:
                                isOptional = true;
                                break;

                            case MultiplicityKind.One:
                                break;

                            case MultiplicityKind.Multiple:
                                isCollection = true;
                                break;

                            default:
                                break;
                            }

                            var modelNavigationProperty = result.CreateNavigationProperty(
                                navigationPropertyName,
                                navigationPropertyExternalName,
                                toComplexType,
                                isOptional,
                                isCollection
                                );
                        }
                    }
                }
            }

            /*
             */
            return(result);
        }
示例#2
0
        public void ReadCsdlEntityType(
            CsdlSchemaModel schemaModel,
            XElement eleEntityType,
            EdmConstants.CSDLConstants csdlConstants,
            ModelErrors errors)
        {
            var entityType = new CsdlEntityTypeModel();

            if (eleEntityType.HasAttributes)
            {
                foreach (var attr in eleEntityType.Attributes())
                {
                    if (attr.IsNamespaceDeclaration)
                    {
                        //
                    }
                    else if (attr.Name == EdmConstants.AttrName)
                    {
                        entityType.Name = attr.Value;
                    }
                    else if (attr.Name == EdmConstants.AttrBaseType)
                    {
                        entityType.BaseType = attr.Value;
                    }
                    else if (attr.Name == EdmConstants.AttrAbstract)
                    {
                        entityType.Abstract = ConvertToBoolean(attr.Value);
                    }
                    else if (attr.Name == EdmConstants.AttrOpenType)
                    {
                        entityType.OpenType = ConvertToBoolean(attr.Value);
                    }
                    else if (attr.Name == EdmConstants.AttrHasStream)
                    {
                        entityType.HasStream = ConvertToBoolean(attr.Value);
                    }
                    else if (CheckAndAddAnnotation(attr, entityType))
                    {
                    }
                    else
                    {
                        errors.AddErrorXmlParsing("ReadCSDLDocument", null, eleEntityType, attr);
                    }
                }
            }
            schemaModel.EntityType.Add(entityType);

            foreach (var ele2 in eleEntityType.Elements())
            {
                if (ele2.Name == csdlConstants.Key)
                {
                    if (ele2.HasAttributes)
                    {
                        foreach (var attr in ele2.Attributes())
                        {
                            if (attr.IsNamespaceDeclaration)
                            {
                                //
                            }
                            else
                            {
                                errors.AddErrorXmlParsing("ReadCSDLDocument", null, eleEntityType, ele2, attr);
                            }
                        }
                    }
                    foreach (var ele3 in ele2.Elements())
                    {
                        var propertyRef = new CsdlPrimaryKeyModel();
                        if (ele3.Name == csdlConstants.PropertyRef)
                        {
                            if (ele3.HasAttributes)
                            {
                                foreach (var attr in ele3.Attributes())
                                {
                                    if (attr.IsNamespaceDeclaration)
                                    {
                                        //
                                    }
                                    else if (attr.Name == EdmConstants.AttrName)
                                    {
                                        propertyRef.Name = attr.Value;
                                    }
                                    else if (CheckAndAddAnnotation(attr, propertyRef))
                                    {
                                    }
                                    else
                                    {
                                        errors.AddErrorXmlParsing("ReadCSDLDocument", null, eleEntityType, ele2, ele3, attr);
                                    }
                                }
                            }
                        }
                        else
                        {
                            errors.AddErrorXmlParsing("ReadCSDLDocument", null, eleEntityType, ele2, ele3);
                        }
                        entityType.Keys.Add(propertyRef);
                    }
                }
                else if (ele2.Name == csdlConstants.Property)
                {
                    var property = new CsdlPropertyModel();
                    if (ele2.HasAttributes)
                    {
                        foreach (var attr in ele2.Attributes())
                        {
                            if (attr.IsNamespaceDeclaration)
                            {
                                //
                            }
                            else if (attr.Name == EdmConstants.AttrName)
                            {
                                property.Name = attr.Value;
                            }
                            else if (attr.Name == EdmConstants.AttrType)
                            {
                                property.TypeName = attr.Value;
                            }
                            else if (attr.Name == EdmConstants.AttrNullable)
                            {
                                property.Nullable = ConvertToBoolean(attr.Value, true);
                            }
                            else if (attr.Name == EdmConstants.AttrMaxLength)
                            {
                                property.MaxLength = ConvertToShort(attr.Value, 0);
                            }
                            else if (attr.Name == EdmConstants.AttrFixedLength)
                            {
                                property.FixedLength = ConvertToBoolean(attr.Value, false);
                            }
                            else if (attr.Name == EdmConstants.AttrPrecision)
                            {
                                property.Precision = ConvertToByte(attr.Value, 0);
                            }
                            else if (attr.Name == EdmConstants.AttrScale)
                            {
                                property.Scale = ConvertToByte(attr.Value, 0);
                            }
                            else if (attr.Name == EdmConstants.AttrUnicode)
                            {
                                property.Unicode = ConvertToBoolean(attr.Value, true);
                            }
                            else if (attr.Name == EdmConstants.AttrCollation)
                            {
                                property.Collation = attr.Value;
                            }
                            else if (attr.Name == EdmConstants.AttrSRID)
                            {
                                property.SRID = attr.Value;
                            }
                            else if (attr.Name == EdmConstants.AttrDefaultValue)
                            {
                                property.DefaultValue = attr.Value;
                            }
                            else if (attr.Name == EdmConstants.AttrConcurrencyMode)
                            {
                                property.ConcurrencyMode = attr.Value;
                            }
                            else if (CheckAndAddAnnotation(attr, property))
                            {
                            }
                            else
                            {
                                errors.AddErrorXmlParsing("ReadCSDLDocument", null, eleEntityType, ele2, attr);
                            }
                        }
                    }
                    entityType.Property.Add(property);
                    foreach (var ele3 in ele2.Elements())
                    {
                        {
                            errors.AddErrorXmlParsing("ReadCSDLDocument", null, eleEntityType, ele2, ele3);
                        }
                    }
                }
                else if (ele2.Name == csdlConstants.NavigationProperty)
                {
                    var navigationProperty = new CsdlNavigationPropertyModel();
                    if (ele2.HasAttributes)
                    {
                        foreach (var attr in ele2.Attributes())
                        {
                            if (attr.IsNamespaceDeclaration)
                            {
                                //
                            }
                            else if (attr.Name == EdmConstants.AttrName)
                            {
                                navigationProperty.Name = attr.Value;
                            }
                            else if (attr.Name == EdmConstants.AttrRelationship)
                            {
                                navigationProperty.RelationshipName = attr.Value;
                            }
                            else if (attr.Name == EdmConstants.AttrFromRole)
                            {
                                navigationProperty.FromRoleName = attr.Value;
                            }
                            else if (attr.Name == EdmConstants.AttrToRole)
                            {
                                navigationProperty.ToRoleName = attr.Value;
                            }
                            else if (attr.Name == EdmConstants.AttrContainsTarget)
                            {
                                navigationProperty.ContainsTarget = ConvertToBoolean(attr.Value, false);
                            }
                            else if (attr.Name == EdmConstants.AttrType)
                            {
                                navigationProperty.TypeName = attr.Value;
                            }
                            else if (attr.Name == EdmConstants.AttrPartner)
                            {
                                navigationProperty.PartnerName = attr.Value;
                            }
                            else if (attr.Name == EdmConstants.AttrNullable)
                            {
                                navigationProperty.Nullable = ConvertToBoolean(attr.Value, true);
                            }
                            else
                            {
                                errors.AddErrorXmlParsing("ReadCSDLDocument", null, eleEntityType, ele2, attr);
                            }
                        }
                    }
                    entityType.NavigationProperty.Add(navigationProperty);

                    foreach (var ele3 in ele2.Elements())
                    {
                        var referentialConstraint = new CsdlReferentialConstraintV4Model();
                        //var propertyRef = new CsdlPrimaryKeyModel();
                        if (ele3.Name == csdlConstants.ReferentialConstraint)
                        {
                            if (ele3.HasAttributes)
                            {
                                foreach (var attr in ele3.Attributes())
                                {
                                    if (attr.IsNamespaceDeclaration)
                                    {
                                        //
                                    }
                                    else if (attr.Name == EdmConstants.AttrProperty)
                                    {
                                        referentialConstraint.PropertyName = attr.Value;
                                    }
                                    else if (attr.Name == EdmConstants.AttrReferencedProperty)
                                    {
                                        referentialConstraint.ReferencedPropertyName = attr.Value;
                                    }
                                    else if (CheckAndAddAnnotation(attr, referentialConstraint))
                                    {
                                    }
                                    else
                                    {
                                        errors.AddErrorXmlParsing("ReadCSDLDocument", null, eleEntityType, ele2, ele3, attr);
                                    }
                                }
                            }
                            foreach (var ele4 in ele3.Elements())
                            {
                                {
                                    errors.AddErrorXmlParsing("ReadCSDLDocument", null, eleEntityType, ele2, ele3, ele4);
                                }
                            }
                        }
                        else
                        {
                            errors.AddErrorXmlParsing("ReadCSDLDocument", null, eleEntityType, ele2, ele3);
                        }
                        navigationProperty.ReferentialConstraint.Add(referentialConstraint);
                    }
                }
                else
                {
                    errors.AddErrorXmlParsing("ReadCSDLDocument", null, eleEntityType, ele2);
                }
            }
        }
示例#3
0
        public ModelComplexType BuildComplexType(EdmxModel edmxModel, ModelSchema modelSchema, CsdlEntityTypeModel entityTypeModel, MetaModelBuilder metaModelBuilder, ModelErrors errors)
        {
            var entityTypeModelName     = entityTypeModel.Name;
            var entityTypeModelFullName = entityTypeModel.FullName;

            var lstFound = modelSchema.ComplexTypes.FindByKey2(entityTypeModelFullName ?? entityTypeModelName);

            if (lstFound.Count == 1)
            {
                return(lstFound[0]);
            }
            else if (lstFound.Count > 1)
            {
                errors.AddErrorOrThrow(new ModelErrorInfo($"found key {entityTypeModelFullName ?? entityTypeModelName} #{lstFound.Count} times.", entityTypeModelFullName ?? entityTypeModelName));
                return(lstFound[0]);
            }

            var modelComplexType = metaModelBuilder.CreateModelComplexType(
                entityTypeModelName,
                entityTypeModelFullName,
                errors);

            if (modelComplexType.Owner == null)
            {
                modelSchema.ComplexTypes.Add(modelComplexType);
            }

            foreach (var property in entityTypeModel.Property)
            {
                ModelScalarType modelScalarType = null;
                ModelScalarType suggestedType   = property.SuggestType(metaModelBuilder);

                // TODO: thinkof EdmxModelBuilder Build ScalarType
                //if (property.ScalarType != null) {
                //    property.ScalarType.FullName
                //}

                modelScalarType = metaModelBuilder.CreateModelScalarType(
                    entityTypeModelName,
                    entityTypeModelFullName,
                    property.Name,
                    null,
                    property.TypeName,
                    suggestedType,
                    property.MaxLength,
                    property.FixedLength,
                    property.Nullable,
                    property.Precision,
                    property.Scale,
                    errors
                    );

                var modelProperty = metaModelBuilder.CreateModelProperty(
                    entityTypeModelName,
                    entityTypeModelFullName,
                    property.Name,
                    null,
                    errors
                    );
                if (modelProperty.Type == null)
                {
                    modelProperty.Type = modelScalarType;
                }
                if (modelProperty.Owner == null)
                {
                    modelComplexType.Properties.Add(modelProperty);
                }
            }
            var primaryKey = entityTypeModel.Keys;

            if (primaryKey.Count > 0)
            {
                var modelIndex = metaModelBuilder.CreateModelIndex(
                    entityTypeModelName,
                    entityTypeModelFullName,
                    "PrimaryKey",
                    null,
                    errors
                    );
                modelIndex.IsPrimaryKey = true;
                if (modelIndex.Owner == null)
                {
                    modelComplexType.Indexes.Add(modelIndex);
                }
                foreach (var keyModel in entityTypeModel.Keys)
                {
                    var modelIndexProperty = metaModelBuilder.CreateModelIndexProperty(
                        entityTypeModelName,
                        entityTypeModelFullName,
                        modelIndex.Name,
                        modelIndex.ExternalName,
                        keyModel.Name,
                        null,
                        //keyModel.Property,
                        errors
                        );
                    if (modelIndexProperty.Owner == null)
                    {
                        modelIndex.Properties.Add(modelIndexProperty);
                    }
                }
            }

            return(modelComplexType);
        }