Ejemplo n.º 1
0
        public EdmModel BuildEdmModel(params IEdmModel[] refModels)
        {
            AddOperations();

            Dictionary <Type, EntityTypeInfo> entityTypeInfos = BuildEntityTypes(refModels);

            foreach (EntityTypeInfo typeInfo in entityTypeInfos.Values)
            {
                if (!typeInfo.IsRefModel)
                {
                    typeInfo.BuildProperties(entityTypeInfos, _enumTypes, _complexTypes);
                }
            }

            foreach (EntityTypeInfo typeInfo in entityTypeInfos.Values)
            {
                if (!typeInfo.IsRefModel)
                {
                    foreach (FKeyInfo fkeyInfo in typeInfo.NavigationClrProperties)
                    {
                        fkeyInfo.BuildNavigationProperty();
                    }
                }
            }

            var edmModel = new EdmModel(false);

            edmModel.AddElements(_enumTypes.Values);
            foreach (KeyValuePair <Type, EdmEnumType> enumType in _enumTypes)
            {
                edmModel.SetClrType(enumType.Value, enumType.Key);
            }

            edmModel.AddElements(_complexTypes.Values);
            foreach (KeyValuePair <Type, EdmComplexType> complexType in _complexTypes)
            {
                edmModel.SetClrType(complexType.Value, complexType.Key);
            }

            var container = new EdmEntityContainer(_dataAdapter.DataContextType.Namespace, _dataAdapter.DataContextType.Name);

            edmModel.SetDataAdapter(container, _dataAdapter);

            var entitySets = new Dictionary <IEdmEntityType, EdmEntitySet>(entityTypeInfos.Count);

            foreach (EntityTypeInfo typeInfo in entityTypeInfos.Values)
            {
                if (!typeInfo.IsRefModel)
                {
                    edmModel.AddElement(typeInfo.EdmType);
                    edmModel.SetClrType(typeInfo.EdmType, typeInfo.ClrType);
                }

                Db.OeEntitySetAdapter entitySetAdapter = _dataAdapter.EntitySetAdapters.Find(typeInfo.ClrType);
                if (entitySetAdapter != null)
                {
                    EdmEntitySet entitySet = container.AddEntitySet(entitySetAdapter.EntitySetName, typeInfo.EdmType);
                    edmModel.SetEntitySetAdapter(entitySet, entitySetAdapter);
                    entitySets.Add(typeInfo.EdmType, entitySet);
                }
            }

            var manyToManyBuilder = new ManyToManyBuilder(edmModel, _metadataProvider, entityTypeInfos);

            foreach (EntityTypeInfo typeInfo in entityTypeInfos.Values)
            {
                if (!typeInfo.IsRefModel)
                {
                    foreach (FKeyInfo fkeyInfo in typeInfo.NavigationClrProperties)
                    {
                        EdmEntitySet principal = entitySets[fkeyInfo.PrincipalInfo.EdmType];
                        EdmEntitySet dependent = entitySets[fkeyInfo.DependentInfo.EdmType];

                        if (fkeyInfo.DependentNavigationProperty == null)
                        {
                            principal.AddNavigationTarget(fkeyInfo.EdmNavigationProperty, dependent);
                        }
                        else
                        {
                            dependent.AddNavigationTarget(fkeyInfo.EdmNavigationProperty, principal);
                            if (fkeyInfo.EdmNavigationProperty.Partner != null)
                            {
                                principal.AddNavigationTarget(fkeyInfo.EdmNavigationProperty.Partner, dependent);
                            }
                        }
                    }

                    manyToManyBuilder.Build(typeInfo);
                }
            }

            foreach (OeOperationConfiguration operationConfiguration in _operationConfigurations)
            {
                if (operationConfiguration.IsEdmFunction)
                {
                    EdmFunction edmFunction = BuildFunction(operationConfiguration, entityTypeInfos);
                    edmModel.AddElement(edmFunction);

                    if (edmFunction.IsBound)
                    {
                        edmModel.SetMethodInfo(edmFunction, operationConfiguration.MethodInfo);
                    }
                    else
                    {
                        container.AddFunctionImport(operationConfiguration.ImportName, edmFunction, edmFunction.EntitySetPath);
                        edmModel.SetIsDbFunction(edmFunction, operationConfiguration.IsDbFunction);
                    }
                }
                else
                {
                    EdmAction edmAction = BuildAction(operationConfiguration, entityTypeInfos);
                    edmModel.AddElement(edmAction);
                    container.AddActionImport(operationConfiguration.ImportName, edmAction);
                    edmModel.SetIsDbFunction(edmAction, operationConfiguration.IsDbFunction);
                }
            }

            edmModel.AddElement(container);
            _dataAdapter.SetEdmModel(edmModel);
            foreach (IEdmModel refModel in refModels)
            {
                edmModel.AddReferencedModel(refModel);
            }

            return(edmModel);
        }
Ejemplo n.º 2
0
        public EdmModel BuildEdmModel()
        {
            Dictionary <Type, EntityTypeInfo> entityTypeInfos = BuildEntityTypes();

            foreach (EntityTypeInfo typeInfo in entityTypeInfos.Values)
            {
                typeInfo.BuildProperties(entityTypeInfos, _enumTypes, _complexTypes);
            }

            foreach (EntityTypeInfo typeInfo in entityTypeInfos.Values)
            {
                foreach (FKeyInfo fkeyInfo in typeInfo.NavigationClrProperties)
                {
                    fkeyInfo.EdmNavigationProperty = CreateNavigationProperty(fkeyInfo);
                }
            }

            var edmModel  = new EdmModel();
            var container = new EdmEntityContainer("Default", "Container");

            edmModel.AddElements(_enumTypes.Values);
            foreach (KeyValuePair <Type, EdmEnumType> enumType in _enumTypes)
            {
                edmModel.SetClrType(enumType.Value, enumType.Key);
            }

            edmModel.AddElements(_complexTypes.Values);
            foreach (KeyValuePair <Type, EdmComplexType> complexType in _complexTypes)
            {
                edmModel.SetClrType(complexType.Value, complexType.Key);
            }

            var entitySets = new Dictionary <IEdmEntityType, EdmEntitySet>(entityTypeInfos.Count);

            foreach (EntityTypeInfo typeInfo in entityTypeInfos.Values)
            {
                edmModel.AddElement(typeInfo.EdmType);
                edmModel.SetClrType(typeInfo.EdmType, typeInfo.ClrType);

                foreach (KeyValuePair <String, Type> pair in _entitySets)
                {
                    if (pair.Value == typeInfo.ClrType)
                    {
                        entitySets.Add(typeInfo.EdmType, container.AddEntitySet(pair.Key, typeInfo.EdmType));
                        break;
                    }
                }
            }

            var manyToManyBuilder = new ManyToManyBuilder(edmModel, _metadataProvider, entityTypeInfos);

            foreach (EntityTypeInfo typeInfo in entityTypeInfos.Values)
            {
                foreach (FKeyInfo fkeyInfo in typeInfo.NavigationClrProperties)
                {
                    EdmEntitySet principal = entitySets[fkeyInfo.PrincipalInfo.EdmType];
                    EdmEntitySet dependent = entitySets[fkeyInfo.DependentInfo.EdmType];

                    if (fkeyInfo.DependentNavigationProperty == null)
                    {
                        principal.AddNavigationTarget(fkeyInfo.EdmNavigationProperty, dependent);
                    }
                    else
                    {
                        dependent.AddNavigationTarget(fkeyInfo.EdmNavigationProperty, principal);
                        if (fkeyInfo.EdmNavigationProperty.Partner != null)
                        {
                            principal.AddNavigationTarget(fkeyInfo.EdmNavigationProperty.Partner, dependent);
                        }
                    }
                }

                manyToManyBuilder.Build(typeInfo);
            }

            foreach (OeOperationConfiguration operationConfiguration in _operationConfigurations)
            {
                if (operationConfiguration.IsEdmFunction)
                {
                    EdmFunction edmFunction = BuildFunction(operationConfiguration, entityTypeInfos);
                    if (edmFunction != null)
                    {
                        edmModel.AddElement(edmFunction);
                        container.AddFunctionImport(operationConfiguration.Name, edmFunction);
                        edmModel.SetIsDbFunction(edmFunction, operationConfiguration.IsDbFunction);
                    }
                }
                else
                {
                    EdmAction edmAction = BuildAction(operationConfiguration, entityTypeInfos);
                    if (edmAction != null)
                    {
                        edmModel.AddElement(edmAction);
                        container.AddActionImport(operationConfiguration.Name, edmAction);
                        edmModel.SetIsDbFunction(edmAction, operationConfiguration.IsDbFunction);
                    }
                }
            }

            edmModel.AddElement(container);
            return(edmModel);
        }