/// <summary> ///Inserts two foreign key refernces into a mapping table /// </summary> public virtual void InsertManyToMany <T>(IDwarf obj1, IDwarf obj2, string alternateTableName = null) { if (!obj1.IsSaved) { obj1.Save(); } if (!obj2.IsSaved) { obj2.Save(); } var type1 = obj1.GetType(); var type2 = obj2.GetType(); var tableName = ManyToManyAttribute.GetManyToManyTableName(type1, type2, alternateTableName); DbContextHelper <T> .ClearCacheForType(type1); DbContextHelper <T> .ClearCacheForType(type2); var preReq = string.Format("IF NOT EXISTS (SELECT * FROM [{0}] WHERE {1}Id = {2} AND {3}Id = {4}) \r\n", tableName, type1.Name, ValueToSqlString(obj1.Id), type2.Name, ValueToSqlString(obj2.Id)); var query = new QueryBuilder <T>() .InsertInto(tableName) .Values(type1.Name + "Id", obj1.Id) .Values(type2.Name + "Id", obj2.Id) .ToQuery(); ExecuteNonQuery <T>(preReq + query); }