public static void CreateRelation <TPrimary, TPrimaryKey, TForeign, TForeignKey>( Database database, UniqueIndex <TPrimary, TPrimaryKey> primaryIndex, IIndex <TForeign, TForeignKey> foreignIndex, Func <TForeignKey, TPrimaryKey> foreignToPrimary, Func <TPrimaryKey, TForeignKey> primaryToForeign, RelationOptions options) where TPrimary : class where TForeign : class { try { database.Tables.CreateRelation( primaryIndex, foreignIndex, foreignToPrimary, primaryToForeign, options); } catch (NMemory.Exceptions.NMemoryException ex) { var message = string.Format("An exception has occurred during table initialization while relating " + "key '{0}' to '{1}'.", primaryIndex, foreignIndex); throw new InvalidOperationException(message, ex); } }
public static void CreateAssociation( Database database, DbRelationInfo relation) { ITable primaryTable = database.GetTable(relation.PrimaryTable); ITable foreignTable = database.GetTable(relation.ForeignTable); IIndex primaryIndex = FindIndex(primaryTable, relation.PrimaryKeyInfo, true); IIndex foreignIndex = FindIndex(foreignTable, relation.ForeignKeyInfo, false); RelationOptions options = new RelationOptions( cascadedDeletion: relation.CascadedDelete); typeof(DatabaseReflectionHelper.WrapperMethods) .GetMethod("CreateRelation") .MakeGenericMethod( primaryTable.EntityType, primaryIndex.KeyInfo.KeyType, foreignTable.EntityType, foreignIndex.KeyInfo.KeyType) .Invoke(null, new object[] { database, primaryIndex, foreignIndex, relation.ForeignToPrimaryConverter, relation.PrimaryToForeignConverter, options }); }
public PharmaDbContext() { //install NMemory nuget from the package manager. var members = this.Tables.Create <Medicines, long>(x => x.Id); //var groups = base.Tables.Create<Group, int>(g => g.Id); this.Medicines = members; // this.Groups = groups; RelationOptions options = new RelationOptions( cascadedDeletion: true); var peopleGroupIdIndex = members.CreateIndex( new RedBlackTreeIndexFactory(), p => p.Id); //Below line creates a relation //this.Tables.CreateRelation( // Medicines.PrimaryKeyIndex, // peopleGroupIdIndex, // x => x==0? -1:0, // x => x, // options); }
public static Relation <TPrimary, TPrimaryKey, TForeign, TForeignKey> CreateRelation <TPrimary, TPrimaryKey, TForeign, TForeignKey, TConstraint1>( this TableCollection tableCollection, IUniqueIndex <TPrimary, TPrimaryKey> primaryIndex, IIndex <TForeign, TForeignKey> foreignIndex, Expression <Func <TPrimary, TConstraint1> > constraint1P, Expression <Func <TForeign, TConstraint1> > constraint1F, RelationOptions options = null) where TPrimary : class where TForeign : class { return(CreateRelation( tableCollection, primaryIndex, foreignIndex, options, new RelationConstraint <TPrimary, TForeign, TConstraint1>(constraint1P, constraint1F))); }
public MyDatabase() { var members = this.Tables.Create <Member, int>(x => x.Id); var groups = base.Tables.Create <Group, int>(g => g.Id); this.Members = members; this.Groups = groups; RelationOptions options = new RelationOptions( cascadedDeletion: true); var peopleGroupIdIndex = members.CreateIndex( new RedBlackTreeIndexFactory(), p => p.GroupId); this.Tables.CreateRelation( groups.PrimaryKeyIndex, peopleGroupIdIndex, x => x ?? -1, x => x, options); }
public static Relation <TPrimary, TPrimaryKey, TForeign, TForeignKey> CreateRelation <TPrimary, TPrimaryKey, TForeign, TForeignKey>( this TableCollection tableCollection, IUniqueIndex <TPrimary, TPrimaryKey> primaryIndex, IIndex <TForeign, TForeignKey> foreignIndex, RelationOptions options, params IRelationContraint[] constraints) where TPrimary : class where TForeign : class { Func <TForeignKey, TPrimaryKey> convertForeignToPrimary = RelationKeyConverterFactory.CreateForeignToPrimaryConverter( primaryIndex.KeyInfo, foreignIndex.KeyInfo, constraints); Func <TPrimaryKey, TForeignKey> convertPrimaryToForeign = RelationKeyConverterFactory.CreatePrimaryToForeignConverter( primaryIndex.KeyInfo, foreignIndex.KeyInfo, constraints); return(tableCollection.CreateRelation(primaryIndex, foreignIndex, convertForeignToPrimary, convertPrimaryToForeign, options)); }
public IRelation AddMemberGroupRelation( bool createMultiField = false, bool useExpressionFactory = false, bool useTuple = false, bool cascadedDeletion = false) { RelationOptions options = new RelationOptions( cascadedDeletion: cascadedDeletion); if (createMultiField) { if (useTuple) { var uniqueIndex = this.groups.CreateUniqueIndex( new RedBlackTreeIndexFactory(), g => new Tuple <int, int>(g.Id, g.Id2)); var foreignIndex = this.members.CreateIndex( new RedBlackTreeIndexFactory(), m => new Tuple <int?, int>(m.GroupId, m.GroupId2)); if (useExpressionFactory) { return(this.Tables.CreateRelation( uniqueIndex, foreignIndex, g => g.Id, m => m.GroupId, g => g.Id2, m => m.GroupId2, options)); } else { return(this.Tables.CreateRelation( uniqueIndex, foreignIndex, x => new Tuple <int, int>(x.Item1.Value, x.Item2), x => new Tuple <int?, int>((int?)x.Item1, x.Item2), options)); } } else { var uniqueIndex = this.groups.CreateUniqueIndex( new RedBlackTreeIndexFactory(), g => new { g.Id, g.Id2 }); var foreignIndex = this.members.CreateIndex( new RedBlackTreeIndexFactory(), m => new { m.GroupId, m.GroupId2 }); if (useExpressionFactory) { return(this.Tables.CreateRelation( uniqueIndex, foreignIndex, g => g.Id, m => m.GroupId, g => g.Id2, m => m.GroupId2, options)); } else { return(this.Tables.CreateRelation( uniqueIndex, foreignIndex, x => new { Id = x.GroupId.Value, Id2 = x.GroupId2 }, x => new { GroupId = (int?)x.Id, GroupId2 = x.Id2 }, options)); } } } else { var foreignIndex = this.members.CreateIndex( new RedBlackTreeIndexFactory(), m => m.GroupId); if (useExpressionFactory) { return(this.Tables.CreateRelation( this.groups.PrimaryKeyIndex, foreignIndex, options, new RelationConstraint <Group, Member, int?>(g => g.Id, m => m.GroupId))); } else { return(this.Tables.CreateRelation( this.groups.PrimaryKeyIndex, foreignIndex, x => x.Value, x => x, options)); } } }