protected static void SetCardinalityValueFromAttribute(XElement element, Action <Cardinality> setAction) { SetStringValueFromAttribute(element, "Multiplicity", multiplicity => setAction(CardinalityStringConverter.CardinalityFromString(multiplicity))); }
public static XElement WriteXElement(SSDLContainer ssdlContainer) { // Instantiate Schema XElement schema = new XElement(ssdlNamespace + "Schema", new XAttribute("Namespace", ssdlContainer.Namespace), new XAttribute("Alias", "Self"), new XAttribute(XNamespace.Xmlns + "store", storeNamespace.NamespaceName), new XAttribute("xmlns", ssdlNamespace.NamespaceName)) .AddAttribute("Provider", ssdlContainer.Provider) .AddAttribute("ProviderManifestToken", ssdlContainer.ProviderManifestToken); // EntityContainer string entityContainerNamespace = string.Concat(ssdlContainer.Namespace, "."); XElement entityContainer = new XElement(ssdlNamespace + "EntityContainer", new XAttribute("Name", ssdlContainer.Name)); schema.Add(entityContainer); // EntityContainer : EntitySets foreach (EntityType entityType in ssdlContainer.EntityTypes) { XElement entitySet = new XElement(ssdlNamespace + "EntitySet", new XAttribute("Name", entityType.EntitySetName), new XAttribute("EntityType", string.Concat(entityContainerNamespace, entityType.Name))) .AddAttribute(entityType.StoreType == StoreType.Views ? null : new XAttribute("Schema", entityType.Schema)) .AddAttribute("Table", entityType.Table) .AddAttribute(storeNamespace, "Name", entityType.StoreName) .AddAttribute(storeNamespace, "Schema", entityType.StoreSchema) .AddAttribute(storeNamespace, "Type", entityType.StoreType) .AddElement(string.IsNullOrEmpty(entityType.DefiningQuery) ? null : new XElement(ssdlNamespace + "DefiningQuery", entityType.DefiningQuery)); entityContainer.Add(entitySet); } // EntityContainer : Associations foreach (Association association in ssdlContainer.AssociationSets) { XElement associationSet = new XElement(ssdlNamespace + "AssociationSet", new XAttribute("Name", association.AssociationSetName), new XAttribute("Association", string.Concat(entityContainerNamespace, association.Name))); string role2Name = association.Role2.Name; // If the association end properties are the same properties if (association.Role1.Name == association.Role2.Name && association.Role1.Type.Name == association.Role2.Type.Name) { role2Name += "1"; } associationSet.Add( new XElement(ssdlNamespace + "End", new XAttribute("Role", association.Role1.Name), new XAttribute("EntitySet", association.Role1.Type.Name)), new XElement(ssdlNamespace + "End", new XAttribute("Role", role2Name), new XAttribute("EntitySet", association.Role2.Type.Name))); entityContainer.Add(associationSet); } // EntityTypes foreach (EntityType entityType in ssdlContainer.EntityTypes) { XElement entityTypeElement = new XElement(ssdlNamespace + "EntityType", new XAttribute("Name", entityType.Name)); XElement keys = new XElement(ssdlNamespace + "Key"); foreach (Property property in entityType.Properties) { // If we have a table then we set a key element if the current property is a primary key or part of a composite key. // AND: If we have a view then we make a composite key of all non-nullable properties of the entity (VS2010 is also doing this). if ((entityType.StoreType == StoreType.Tables && property.IsKey) || (entityType.StoreType == StoreType.Views && property.Nullable == false)) { keys.Add(new XElement(ssdlNamespace + "PropertyRef", new XAttribute("Name", property.Name))); } } if (!keys.IsEmpty) { entityTypeElement.Add(keys); } foreach (Property property in entityType.Properties) { entityTypeElement.Add(new XElement(ssdlNamespace + "Property", new XAttribute("Name", property.Name), new XAttribute("Type", property.Type)) .AddAttribute("Collation", property.Collation) .AddAttribute("DefaultValue", property.DefaultValue) .AddAttribute("FixedLength", property.FixedLength) .AddAttribute("MaxLength", property.MaxLength) .AddAttribute("Nullable", property.Nullable) .AddAttribute("Precision", property.Precision) .AddAttribute("Scale", property.Scale) .AddAttribute("StoreGeneratedPattern", property.StoreGeneratedPattern) .AddAttribute(storeNamespace, "Name", property.StoreName) .AddAttribute(storeNamespace, "Schema", property.StoreSchema) .AddAttribute(storeNamespace, "Type", property.StoreType) .AddAttribute("Unicode", property.Unicode)); } schema.AddElement(entityTypeElement); } foreach (Association association in ssdlContainer.AssociationSets) { string role2Name = association.Role2.Name; // If the association end properties are the same properties if (association.Role1.Name == association.Role2.Name && association.Role1.Type.Name == association.Role2.Type.Name) { role2Name += "1"; } XElement associationElement = new XElement(ssdlNamespace + "Association", new XAttribute("Name", association.Name), new XElement(ssdlNamespace + "End", new XAttribute("Role", association.Role1.Name), new XAttribute("Type", string.Concat(entityContainerNamespace, association.Role1.Type.Name)), new XAttribute("Multiplicity", CardinalityStringConverter.CardinalityToString(association.Role1.Cardinality))), new XElement(ssdlNamespace + "End", new XAttribute("Role", role2Name), new XAttribute("Type", string.Concat(entityContainerNamespace, association.Role2.Type.Name)), new XAttribute("Multiplicity", CardinalityStringConverter.CardinalityToString(association.Role2.Cardinality)))); string dependentRoleName = association.DependantRole.Name; // If the association end properties are the same properties if (association.PrincipalRole.Name == association.DependantRole.Name && association.PrincipalRole.Type.Name == association.DependantRole.Type.Name) { dependentRoleName += "1"; } XElement principalRoleElement = new XElement(ssdlNamespace + "Principal", new XAttribute("Role", association.PrincipalRole.Name)); foreach (Property property in association.PrincipalRole.Properties) { principalRoleElement.Add(new XElement(ssdlNamespace + "PropertyRef", new XAttribute("Name", property.Name))); } XElement dependentRoleElement = new XElement(ssdlNamespace + "Dependent", new XAttribute("Role", dependentRoleName)); foreach (Property property in association.DependantRole.Properties) { dependentRoleElement.Add(new XElement(ssdlNamespace + "PropertyRef", new XAttribute("Name", property.Name))); } XElement referentialConstraintElement = new XElement(ssdlNamespace + "ReferentialConstraint", principalRoleElement, dependentRoleElement); associationElement.Add(referentialConstraintElement); schema.Add(associationElement); } foreach (Function function in ssdlContainer.Functions) { XElement functionElement = new XElement(ssdlNamespace + "Function", new XAttribute("Name", function.Name)) .AddAttribute("Aggregate", function.Aggregate) .AddAttribute("BuiltIn", function.BuiltIn) .AddAttribute("CommandText", function.CommandText) .AddAttribute("IsComposable", function.IsComposable) .AddAttribute("NiladicFunction", function.NiladicFunction) .AddAttribute("ReturnType", function.ReturnType) .AddAttribute("StoreFunctionName", function.StoreFunctionName) .AddAttribute("ParameterTypeSemantics", function.ParameterTypeSemantics) .AddAttribute("Schema", function.Schema) .AddAttribute(storeNamespace, "Name", function.StoreName) .AddAttribute(storeNamespace, "Schema", function.StoreSchema) .AddAttribute(storeNamespace, "Type", function.StoreType); foreach (FunctionParameter functionParameter in function.Parameters) { functionElement.Add(new XElement(ssdlNamespace + "Parameter", new XAttribute("Name", functionParameter.Name), new XAttribute("Type", functionParameter.Type), new XAttribute("Mode", functionParameter.Mode)) .AddAttribute("MaxLength", functionParameter.MaxLength) .AddAttribute("Precision", functionParameter.Precision) .AddAttribute("Scale", functionParameter.Scale) .AddAttribute(storeNamespace, "Name", functionParameter.StoreName) .AddAttribute(storeNamespace, "Schema", functionParameter.StoreSchema) .AddAttribute(storeNamespace, "Type", functionParameter.StoreType)); } schema.Add(functionElement); } return(schema); }
public static XElement Write(CSDLContainer csdlContainer) { // Instantiate Schema XElement schema = new XElement(csdlNamespace + "Schema", new XAttribute("Namespace", csdlContainer.Namespace), new XAttribute("Alias", csdlContainer.Alias), new XAttribute(XNamespace.Xmlns + "annotation", csdlAnnotationNamespace.NamespaceName), new XAttribute("xmlns", csdlNamespace.NamespaceName)); // EntityContainer string entityContainerNamespace = string.Concat(csdlContainer.Namespace, "."); XElement entityContainer = new XElement(csdlNamespace + "EntityContainer", new XAttribute("Name", csdlContainer.Name)); schema.Add(entityContainer); // EntityContainer : EntitySets foreach (EntityType entityType in csdlContainer.EntitySets) { XElement entitySetElement = new XElement(csdlNamespace + "EntitySet", new XAttribute("Name", entityType.EntitySetName), new XAttribute("EntityType", string.Concat(entityContainerNamespace, entityType.Name))); //.AddAttribute(csdlCodeGenerationNamespace, "GetterAccess", entityType.EntitySetVisibility); // Not available in EF 4.0 entityContainer.Add(entitySetElement); } // EntityContainer : AssociationSets foreach (Association association in csdlContainer.Associations) { XElement associationSetElement = new XElement(csdlNamespace + "AssociationSet", new XAttribute("Name", association.AssociationSetName), new XAttribute("Association", string.Concat(entityContainerNamespace, association.Name))); associationSetElement.Add( new XElement(csdlNamespace + "End", new XAttribute("Role", association.PropertyEnd1Role), new XAttribute("EntitySet", association.PropertyEnd1.EntityType.EntitySetName)), new XElement(csdlNamespace + "End", new XAttribute("Role", association.PropertyEnd2Role), new XAttribute("EntitySet", association.PropertyEnd2.EntityType.EntitySetName))); entityContainer.AddElement(associationSetElement); } // EntityContainer : FunctionImports foreach (Function function in csdlContainer.Functions) { XElement functionElement = new XElement(csdlNamespace + "FunctionImport", new XAttribute("Name", function.Name)) .AddAttribute("EntitySet", function.EntityType == null ? null : function.EntityType.EntitySetName) .AddAttribute("ReturnType", function.ReturnType); foreach (FunctionParameter functionParameter in function.Parameters) { functionElement.AddElement(new XElement(csdlNamespace + "Paramter", new XAttribute("Name", functionParameter.Name), new XAttribute("Type", functionParameter.Type)) .AddAttribute("MaxLength", functionParameter.MaxLength) .AddAttribute("Mode", functionParameter.Mode) .AddAttribute("Precision", functionParameter.Precision) .AddAttribute("Scale", functionParameter.Scale)); } entityContainer.AddElement(functionElement); } // ComplexTypes foreach (ComplexType complexType in csdlContainer.ComplexTypes) { XElement complexTypeElement = new XElement(csdlNamespace + "ComplexType", new XAttribute("Name", complexType.Name)); //.AddAttribute(new XAttribute(csdlCodeGenerationNamespace + "TypeAccess", complexType.Visibility)); // Not available in EF 4.0 complexTypeElement.Add(WriteScalarProperties(complexType)); complexTypeElement.Add(WriteComplexProperties(complexType, string.Concat(csdlContainer.Alias, "."))); schema.AddElement(complexTypeElement); } // EntityTypes foreach (EntityType entityType in csdlContainer.EntityTypes) { XElement entityTypeElement = new XElement(csdlNamespace + "EntityType") .AddAttribute("Name", entityType.Name) //.AddAttribute(csdlCodeGenerationNamespace, "TypeAccess", entityType.Visibility) // Not available in EF 4.0 .AddAttribute("BaseType", entityType.BaseType == null ? null : string.Concat(entityContainerNamespace, entityType.BaseType.Name)) .AddAttribute("Abstract", entityType.Abstract); if (entityType.SpecificKeys.Any()) { XElement keyElement = new XElement(csdlNamespace + "Key"); entityType.ScalarProperties.Where(sp => sp.IsKey).ForEach(scalarProperty => { keyElement.AddElement(new XElement(csdlNamespace + "PropertyRef") .AddAttribute("Name", scalarProperty.Name)); }); entityTypeElement.AddElement(keyElement); } entityTypeElement.Add(WriteScalarProperties(entityType)); entityTypeElement.Add(WriteComplexProperties(entityType, string.Concat(csdlContainer.Alias, "."))); // EntityType : NavigationProperties entityType.NavigationProperties.Where(np => np.Generate).ForEach(navigationProperty => { entityTypeElement.AddElement(new XElement(csdlNamespace + "NavigationProperty") .AddAttribute("Name", navigationProperty.Name) .AddAttribute("Relationship", string.Concat(entityContainerNamespace, navigationProperty.Association.Name)) .AddAttribute("FromRole", navigationProperty.Association.GetRoleName(navigationProperty)) .AddAttribute("ToRole", navigationProperty.Association.GetRoleName(navigationProperty.Association.PropertiesEnd.First(role => role != navigationProperty)))); //.AddAttribute(csdlCodeGenerationNamespace, "GetterAccess", navigationProperty.GetVisibility) // Not available in EF 4.0 //.AddAttribute(csdlCodeGenerationNamespace, "SetterAccess", navigationProperty.SetVisibility)); }); schema.AddElement(entityTypeElement); } // Associations foreach (Association association in csdlContainer.Associations) { XElement associationElement = new XElement(csdlNamespace + "Association") .AddAttribute("Name", association.Name); associationElement.AddElement(new XElement(csdlNamespace + "End") .AddAttribute("Role", association.PropertyEnd1Role) .AddAttribute("Type", string.Concat(entityContainerNamespace, association.PropertyEnd1.EntityType.Name)) .AddAttribute("Multiplicity", CardinalityStringConverter.CardinalityToString(association.PropertyEnd1.Cardinality))); associationElement.AddElement(new XElement(csdlNamespace + "End") .AddAttribute("Role", association.PropertyEnd2Role) .AddAttribute("Type", string.Concat(entityContainerNamespace, association.PropertyEnd2.EntityType.Name)) .AddAttribute("Multiplicity", CardinalityStringConverter.CardinalityToString(association.PropertyEnd2.Cardinality))); if (association.PrincipalRole != null) { XElement referentialConstraintElement = new XElement(csdlNamespace + "ReferentialConstraint"); XElement principalElement = (new XElement(csdlNamespace + "Principal") .AddAttribute("Role", association.PrincipalRole)); foreach (ScalarProperty propertyRef in association.PrincipalProperties) { principalElement.AddElement(new XElement(csdlNamespace + "PropertyRef").AddAttribute("Name", propertyRef.Name)); } XElement dependentElement = (new XElement(csdlNamespace + "Dependent") .AddAttribute("Role", association.DependentRole)); foreach (ScalarProperty propertyRef in association.DependentProperties) { dependentElement.AddElement(new XElement(csdlNamespace + "PropertyRef").AddAttribute("Name", propertyRef.Name)); } referentialConstraintElement.AddElement(principalElement); referentialConstraintElement.AddElement(dependentElement); associationElement.AddElement(referentialConstraintElement); } schema.AddElement(associationElement); } return(schema); }