void CheckRelation(Test test, RelInfo ri) { if (!ri.HasTable) { return; } NDO.Mapping.Class clMapping = mapping.FindClass("RelationTestClasses." + test.OwnClass.Name); if (clMapping == null) { return; } NDO.Mapping.Relation rel = (NDO.Mapping.Relation)clMapping.Relations.FirstOrDefault(); Debug.Assert(rel != null, "Relation mapping not found"); if (rel == null) { throw new Exception("Cant find relation 0 of" + test.OwnClass.Name); } NDO.Mapping.Class derivedMapping; NDO.Mapping.Relation derivedRel = null; if (ri.OwnPoly) { derivedMapping = mapping.FindClass("RelationTestClasses." + test.OwnDerivedClass.Name); if (derivedMapping == null) { return; } derivedRel = (NDO.Mapping.Relation)derivedMapping.Relations.FirstOrDefault(); if (rel == null) { throw new Exception("Cant find relation 0 of" + test.OwnDerivedClass.Name); } } if (rel.MappingTable == null || ri.OwnPoly && derivedRel.MappingTable == null) { string tableName = null; if (test.OwnClass.Name.CompareTo(test.OtherClass.Name) < 0) { tableName = "rel" + test.OwnClass.Name + test.OtherClass.Name; } else { tableName = "rel" + test.OtherClass.Name + test.OwnClass.Name; } ForeignKeyColumn fkColumn = rel.NewForeignKeyColumn(); fkColumn.Name = "ID" + test.OwnClass.Name; if (ri.OwnPoly) { rel.ForeignKeyTypeColumnName = "TC" + test.OwnClass.Name; } else { rel.ForeignKeyTypeColumnName = null; } rel.MappingTable = new NDO.Mapping.MappingTable(rel); rel.MappingTable.TableName = tableName; fkColumn = rel.MappingTable.NewForeignKeyColumn(); fkColumn.Name = "ID" + test.OtherClass.Name; if (ri.OtherPoly) { rel.MappingTable.ChildForeignKeyTypeColumnName = "TC" + test.OtherClass.Name; } else { rel.MappingTable.ChildForeignKeyTypeColumnName = null; } rel.MappingTable.ConnectionId = clMapping.ConnectionId; if (ri.OwnPoly) { ForeignKeyColumn dfkColumn = (ForeignKeyColumn)derivedRel.ForeignKeyColumns.FirstOrDefault(); dfkColumn.Name = ((ForeignKeyColumn)rel.ForeignKeyColumns.FirstOrDefault()).Name; derivedRel.ForeignKeyTypeColumnName = rel.ForeignKeyTypeColumnName; derivedRel.MappingTable = rel.MappingTable; } } }
/// <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); }