internal void RemapForeignMappingTable(string myShortName, string refShortName, bool ownTypeIsPoly, bool otherTypeIsPoly, MappingTableAttribute mappingTableAttribute) { if (this.foreignRelation == null) { return; } if (foreignRelation.MappingTable == null) { foreignRelation.AddMappingTable(myShortName, refShortName, ownTypeIsPoly, mappingTableAttribute); if (otherTypeIsPoly) { foreignRelation.ForeignKeyTypeColumnName = "TC" + refShortName; } } string frFkcName = "ID" + refShortName; // This is going to be the r.ForeignKeyColumnName of the foreign relation string frFtcName = null; if (ownTypeIsPoly) { frFtcName = "TC" + myShortName; // This is going to be the r.MappingTable.ChildForeignKeyColumnName of the foreign relation } if (relationName != string.Empty) { frFkcName += "_" + relationName; if (ownTypeIsPoly) { frFtcName += "_" + relationName; } } ForeignKeyColumn forFkColumn = foreignRelation.ForeignKeyColumns.FirstOrDefault(); forFkColumn.Name = frFkcName; foreignRelation.MappingTable.ChildForeignKeyTypeColumnName = frFtcName; }
/// <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); }