/// <summary> /// /// </summary> /// <example> /// Xml example: /// <![CDATA[ /// <Property Name="Shooting" Type="Movies.Location" Nullable="false" /> /// ]]> /// </example> ///<param name="mmodel"></param> ///<param name="entityType"> </param> ///<param name="scP"></param> ///<returns></returns> public static MetaComplexProperty Parse(MetaModel mmodel, MetaEntityType entityType, XElement scP) { var p = new MetaComplexProperty { EntityType = entityType, FullName = entityType.FullName.Dot(scP.Att("Name")), Name = scP.Att("Name"), Nullable = scP.Att("Name").ToLower() == "true" }; mmodel.ComplexProperties.Add(p.FullName, p); MetaComplexType ct; if (mmodel.ComplexTypes.ContainsKey(scP.Att("Type"))) ct = mmodel.ComplexTypes[scP.Att("Type")]; else { var ctNode = MetaComplexType.FindNode(mmodel, scP.Att("Type")); if(ctNode == null) throw new Exception("Couldn't find the ComplexType " + scP.Att("Type") + ". The type was used in " + entityType.FullName.Dot(scP.Att("Name"))); ct = MetaComplexType.Parse(mmodel, ctNode); } p.ComplexType = ct; return p; }
/* * <Association Name="FK_movies_genres"> <End Type="Movies.Genre" Role="genre" Multiplicity="0..1" /> <End Type="Movies.Movie" Role="movie" Multiplicity="*" /> <ReferentialConstraint> <Principal Role="genre"> <PropertyRef Name="Id" /> </Principal> <Dependent Role="movie"> <PropertyRef Name="GenreId" /> </Dependent> </ReferentialConstraint> </Association> * */ public static MetaAssociationRole Parse(MetaModel mmodel, MetaEntityType fromET, XElement navP) { var fullName = navP.Att("Relationship") + '.' + navP.Att("ToRole"); if(mmodel.AssociationRoles.ContainsKey(fullName)) return mmodel.AssociationRoles[fullName]; // navP : <NavigationProperty Name="Movies" Relationship="Movies.FK_movies_genres" FromRole="genre" ToRole="movie" /> var a = new MetaAssociationRole(); a.FullName = fullName; a.Name = a.FullName.LastPart(); a.NavigationProperty = (from p in fromET.NavigationProperties where p.Name == navP.Att("Name") select p).FirstOrDefault(); mmodel.AssociationRoles.Add(a.FullName, a); var assNode = mmodel.EdmxFile.Concept.D("Association").WithName(navP.Att("Relationship").LastPart()); var role = assNode.Es("End").FirstWhere("Role", navP.Att("ToRole")); var toE = mmodel.EntityTypes.ContainsKey(role.Att("Type")) ? mmodel.EntityTypes[role.Att("Type")] : MetaEntityType.Parse(mmodel, role.Att("Type")); a.MultiplicityValue = role.Att("Multiplicity"); a.ToEntityType = toE; return a; }
public static bool IsComplexPropertyNode(MetaModel mmodel, XElement scP) { if (mmodel.ComplexTypes.ContainsKey(scP.Att("Type"))) return true; return mmodel.EdmxFile.Concept.Es("ComplexType").FirstWhere("Name", scP.Att("Type").LastPart()) != null; }
public static MetaContainer Parse(MetaModel mmodel, string name) { var node = mmodel.EdmxFile.Concept.D(NodeName).WithName(name); if (node == null) throw new Exception("Couldn't find the container " + name); return Parse(mmodel, node); }
public static MetaEntitySet Parse(MetaModel mmodel, string fullName) { var node = mmodel.EdmxFile.Concept.D(NodeName).WithName(fullName.LastPart()); if (node == null) throw new Exception("Couldn't find the EntitySet " + fullName); return Parse(mmodel, null, node); }
/// <summary> /// /// </summary> /// <example> /// Xml example: /// <![CDATA[ /// <Property Type="String" Name="City" MaxLength="50" FixedLength="false" Unicode="true" /> /// ]]> /// </example> /// <param name="file"></param> /// <param name="ct"></param> /// <param name="prop"></param> /// <returns></returns> public static MetaComplexTypeProperty Parse(MetaModel metaModel, MetaComplexType ct, XElement prop) { var p = new MetaComplexTypeProperty { FullName = ct.FullName.Dot(prop.Att("Name")), Name = prop.Att("Name"), Type = prop.Att("Type"), ComplexType = ct }; return p; }
public static MetaContainer Parse(MetaModel mmodel, XElement node) { if(node.Name.LocalName != NodeName) throw new Exception("Invalid Node. To parse a " + NodeName + " you need to provide an XElement with the same name."); var c = new MetaContainer(); c.Name = node.Att("Name"); c.FullName = node.Parent.Att("Namespace") + '.' + c.Name; foreach (var eSet in node.Es("EntitySet")) { c.EntitySets.Add(MetaEntitySet.Parse(mmodel, c, eSet)); } mmodel.Containers[c.FullName] = c; return c; }
public static MetaScalarProperty Parse(MetaModel mmodel, MetaEntityType et, XElement node) { var fullName = et.FullName + '.' + node.Att("Name"); if (mmodel.ScalarProperties.ContainsKey(fullName)) return mmodel.ScalarProperties[fullName]; var sp = new MetaScalarProperty() { FullName = fullName, Name = node.Att("Name"), Nullable = node.Att("Nullable") == "true", Type = node.Att("Type"), EntityType = et }; mmodel.ScalarProperties.Add(fullName, sp); return sp; }
// <EntitySet Name="Genres" EntityType="Movies.Genre" /> // <EntitySet Name="Movies" EntityType="Movies.Movie" /> // <EntitySet Name="People" EntityType="Movies.Person" /> // <EntitySet Name="Roles" EntityType="Movies.Role" /> public static MetaEntitySet Parse(MetaModel mmodel, MetaContainer c, XElement node) { if (node.Name.LocalName != NodeName) throw new Exception("Invalid Node. To parse an " + NodeName + " you need to provide an XElement with the same node name."); var fullName = c.FullName + '.' + node.Att("Name"); if (mmodel.EntitySets.ContainsKey(fullName)) return mmodel.EntitySets[fullName]; var m = new MetaEntitySet { FullName = fullName, Name = node.Att("Name"), Container = c }; mmodel.EntitySets.Add(fullName, m); m.EntityType = MetaEntityType.Parse(mmodel, node.Att("EntityType"), m); /* todo: process store // check for mapping var mapEnt = file.Mapping.d("EntitySetMapping").WithName(m.Name); if (mapEnt != null) { // we have a mapping! so we should have a MappingFrag... var mapFrag = mapEnt.d("MappingFragment").FirstOrDefault(); if (mapFrag != null) // load the store m.Store = MetaEntitySetStore.Parse(file, mapFrag.Att("StoreEntitySet")); } if (m.Store != null) { m.Store.EntityTypeStore = m.EntityType.Store; } */ return m; }
/// <summary> /// /// </summary> /// <example> /// Xml Example: /// <![CDATA[ /// <ComplexType Name="Location"> /// <Property Type="String" Name="City" MaxLength="50" FixedLength="false" Unicode="true" /> /// <Property Type="String" Name="State" MaxLength="50" FixedLength="false" Unicode="true" /> /// <Property Type="String" Name="Country" MaxLength="50" FixedLength="false" Unicode="true" /> /// </ComplexType> /// ]]> /// </example> /// <param name="metaModel"></param> /// <param name="cType"></param> /// <returns></returns> public static MetaComplexType Parse(MetaModel metaModel, XElement cType) { var fullName = metaModel.Namespace.Dot(cType.Att("Name")); if (metaModel.ComplexTypes.ContainsKey(fullName)) return metaModel.ComplexTypes[fullName]; var ct = new MetaComplexType(); ct.FullName = fullName; ct.Name = cType.Att("Name"); foreach (var prop in cType.Es("Property")) { ct.Properties.Add(MetaComplexTypeProperty.Parse(metaModel, ct, prop)); } metaModel.ComplexTypes.Add(ct.FullName, ct); return ct; }
// <NavigationProperty Name="Movies" Relationship="Movies.FK_movies_genres" FromRole="genre" ToRole="movie" /> public static MetaNavigationProperty Parse(MetaModel mmodel, MetaEntityType et, XElement node) { var fullName = et.FullName + '.' + node.Att("Name"); if (mmodel.NavigationProperties.ContainsKey(fullName)) return mmodel.NavigationProperties[fullName]; var m = new MetaNavigationProperty { FullName = fullName, Name = node.Att("Name") }; mmodel.NavigationProperties.Add(fullName, m); m.AssociationRole = MetaAssociationRole.Parse(mmodel, et, node); m.EntityType = m.AssociationRole.ToEntityType; return m; }
public void TestMetaModelLoad() { var edmx = new EdmxFile(@"Movies.edmx"); var model = new MetaModel(edmx); }
public static List<MetaEntitySet> ParseAll(MetaModel mmodel) { // Eg: <EntityContainer Name="MoviesContainer" annotation:LazyLoadingEnabled="true"> var container = mmodel.EdmxFile.Concept.E("EntityContainer"); if (container == null) throw new Exception("Couldn't find the EntityContainer. The edmx file appears to be invalid."); var list = (from n in container.D("EntitySet") select Parse(mmodel, container.Att("Name") + '.' + n.Att("Name"))).ToList(); return list; }
public static XElement FindNode(MetaModel mmodel, string name) { return mmodel.EdmxFile.Concept.Es("ComplexType").WithName(name.LastPart()); }
public static MetaEntityType Parse(MetaModel mmodel, string fullName, MetaEntitySet eSet = null) { if (mmodel.EntityTypes.ContainsKey(fullName)) return mmodel.EntityTypes[fullName]; var m = new MetaEntityType { FullName = fullName, Name = fullName.LastPart(), EntitySet = eSet }; // need to add before processing related meta-models mmodel.EntityTypes.Add(m.FullName, m); var node = mmodel.EdmxFile.Concept.D(NodeName).WithName(m.Name); var keyNames = (from k in node.D("PropertyRef") select k.Att("Name")).ToList(); // parsing properties foreach (var scP in node.D("Property")) { // Property elements with a Type attribute reprents a ComplexProperty if (MetaComplexProperty.IsComplexPropertyNode(mmodel, scP)) { m.ComplexProperties.Add(MetaComplexProperty.Parse(mmodel, m, scP)); } else { var sp = MetaScalarProperty.Parse(mmodel, m, scP); sp.isKey = keyNames.Contains(scP.Att("Name")); m.Properties.Add(sp); } } foreach (var navP in node.D("NavigationProperty")) { m.NavigationProperties.Add(MetaNavigationProperty.Parse(mmodel, m, navP)); } // todo: process store return m; }