internal Relation(XmlNode relNode, Class parent) : base(relNode, parent) { fieldName = relNode.Attributes["FieldName"].Value; referencedTypeName = relNode.Attributes["ReferencedTypeName"].Value; if (relNode.Attributes["AccessorName"] != null) { this.accessorName = relNode.Attributes["AccessorName"].Value; } if (relNode.Attributes["ForeignKeyTypeColumnName"] != null && relNode.Attributes["ForeignKeyTypeColumnName"].Value != string.Empty) { this.foreignKeyTypeColumnName = relNode.Attributes["ForeignKeyTypeColumnName"].Value; } if (relNode.Attributes["ForeignKeyColumnName"] != null) // Old mapping { ForeignKeyColumn fkColumn = new ForeignKeyColumn(this); fkColumn.Name = relNode.Attributes["ForeignKeyColumnName"].Value; this.foreignKeyColumns.Add(fkColumn); } else { XmlNodeList nl = relNode.SelectNodes(parent.Parent.selectForeignKeyColumns); foreach (XmlNode fkcNode in nl) { this.foreignKeyColumns.Add(new ForeignKeyColumn(fkcNode, this)); } } XmlNode mtNode = relNode.SelectSingleNode(Parent.Parent.selectMappingTable, Parent.Parent.nsmgr); if (null != mtNode) { mappingTable = new MappingTable(mtNode, this); } if (null != relNode.Attributes["RelationName"]) { relationName = relNode.Attributes["RelationName"].Value; } else { relationName = string.Empty; } }
private bool ForeignKeyColumnsAreDifferent(MappingTable t1, MappingTable t2) { var t1ChildForeignKeyColumns = t1.ChildForeignKeyColumns.ToList(); var t2ChildForeignKeyColumns = t2.ChildForeignKeyColumns.ToList(); if (t1ChildForeignKeyColumns.Count != t2ChildForeignKeyColumns.Count) { return(true); } for (int i = 0; i < t1ChildForeignKeyColumns.Count; i++) { ForeignKeyColumn fkc1 = t1ChildForeignKeyColumns[i]; ForeignKeyColumn fkc2 = t2ChildForeignKeyColumns[i]; if (ColumnsAreDifferent(fkc1, fkc2)) { return(true); } } return(false); }
private bool ForeignKeyColumnsAreDifferent(Relation r1, Relation r2) { var r1ForeignKeyColumns = r1.ForeignKeyColumns.ToList(); var r2ForeignKeyColumns = r2.ForeignKeyColumns.ToList(); if (r1ForeignKeyColumns.Count != r2ForeignKeyColumns.Count) { return(true); } for (int i = 0; i < r1ForeignKeyColumns.Count; i++) { ForeignKeyColumn fkc1 = (ForeignKeyColumn)r1ForeignKeyColumns[i]; ForeignKeyColumn fkc2 = (ForeignKeyColumn)r2ForeignKeyColumns[i]; if (ColumnsAreDifferent(fkc1, fkc2)) { return(true); } } return(false); }
internal void AddMappingTable(string typeShortName1, string typeShortName2, bool otherTypeIsPoly, MappingTableAttribute mappingTableAttribute) { this.MappingTable = new MappingTable(this); ForeignKeyColumn fkColumn = this.MappingTable.NewForeignKeyColumn(); fkColumn.Name = "ID" + typeShortName1; if (otherTypeIsPoly) { this.MappingTable.ChildForeignKeyTypeColumnName = "TC" + typeShortName1; } if (this.RelationName != null && this.RelationName != string.Empty) { fkColumn.Name += "_" + this.RelationName; if (otherTypeIsPoly) { this.MappingTable.ChildForeignKeyTypeColumnName += "_" + this.RelationName; } } if (mappingTableAttribute != null && mappingTableAttribute.TableName != null) { this.MappingTable.TableName = mappingTableAttribute.TableName; } else { if (typeShortName1.CompareTo(typeShortName2) < 0) { this.MappingTable.TableName = "rel" + typeShortName1 + typeShortName2; } else { this.MappingTable.TableName = "rel" + typeShortName2 + typeShortName1; } } this.MappingTable.ConnectionId = ((Connection)Parent.Parent.Connections.First()).ID; }
/// <summary> /// Removes a ChildForeignKeyColumn fromthe mapping table. /// </summary> /// <param name="fkc"></param> public void RemoveChildForeignKeyColumn(ForeignKeyColumn fkc) { this.childForeignKeyColumns.Remove(fkc); }
/// <summary> /// Adds a default relation mapping. /// </summary> /// <param name="fieldName">Name of the field</param> /// <param name="referencedTypeName">Type name of the referenced class</param> /// <param name="isElement">True, if multiplicity is 1</param> /// <param name="relationName">Optional relation name</param> /// <param name="ownTypeIsPoly">True, if the class, containing the field, has a persistent base class</param> /// <param name="otherTypeIsPoly">True, if the related type has a persistent base class</param> /// <param name="mappingTableAttribute">If not null, the mapping information comes from this attribute.</param> /// <returns>A new constructed <code>Relation</code> object</returns> public Relation AddStandardRelation(string fieldName, string referencedTypeName, bool isElement, string relationName, bool ownTypeIsPoly, bool otherTypeIsPoly, MappingTableAttribute mappingTableAttribute) { // if (null != Parent) // Parent.this.Changed = true; Relation r = new Relation(this); r.FieldName = fieldName; r.ReferencedTypeName = referencedTypeName; //r.parent = this; r.RelationName = relationName; r.Multiplicity = isElement ? RelationMultiplicity.Element : RelationMultiplicity.List; int pos = referencedTypeName.LastIndexOf('.'); string refShortName = referencedTypeName.Substring(pos + 1); refShortName = refShortName.Replace("`", string.Empty); pos = this.FullName.LastIndexOf('.'); string myShortName = this.FullName.Substring(pos + 1); myShortName = myShortName.Replace("`", string.Empty); Relation foreignRelation = r.ForeignRelation; ForeignKeyColumn fkColumn = r.NewForeignKeyColumn(); // Element->x AND no MappingTable if (isElement && mappingTableAttribute == null && !(foreignRelation != null && foreignRelation.MappingTable != null)) { r.MappingTable = null; // Foreign Key is in the own table and points to rows of the other table fkColumn.Name = "ID" + refShortName; if (otherTypeIsPoly) { r.ForeignKeyTypeColumnName = "TC" + refShortName; } if (relationName != string.Empty) { fkColumn.Name += "_" + relationName; if (otherTypeIsPoly) { r.ForeignKeyTypeColumnName += "_" + relationName; } } } else // List or (Element with Mapping Table) { // These are the reasons for creating a mapping table: // 1. The MappingTableAttribute demands it // 2. We have a n:n relationship // 3. We have a 1:n relationship and the other type is poly // 4. The relation is bidirectional and the other side demands a mapping table bool needsMappingTable = mappingTableAttribute != null || null != foreignRelation && (foreignRelation.Multiplicity == RelationMultiplicity.List && r.Multiplicity == RelationMultiplicity.List) || otherTypeIsPoly && r.Multiplicity == RelationMultiplicity.List || null != foreignRelation && foreignRelation.MappingTable != null; // Foreign Key points to rows of our own table fkColumn.Name = "ID" + myShortName; if (ownTypeIsPoly && needsMappingTable) { r.ForeignKeyTypeColumnName = "TC" + myShortName; } else if (otherTypeIsPoly && !needsMappingTable) { r.ForeignKeyTypeColumnName = "TC" + refShortName; } if (relationName != string.Empty) { fkColumn.Name += "_" + relationName; if (ownTypeIsPoly) { r.ForeignKeyTypeColumnName += "_" + relationName; } } if (needsMappingTable) { r.AddMappingTable(refShortName, myShortName, otherTypeIsPoly, mappingTableAttribute); r.RemapForeignMappingTable(myShortName, refShortName, ownTypeIsPoly, otherTypeIsPoly, mappingTableAttribute); } else { r.MappingTable = null; } } this.relations.Add(r); return(r); }
/// <summary> /// Removes a foreign key column from the relation. /// </summary> /// <param name="fkc"></param> public void RemoveForeignKeyColumn(ForeignKeyColumn fkc) { this.foreignKeyColumns.Remove(fkc); }