コード例 #1
0
        public void CreateForeignToPrimaryConversion()
        {
            DefaultKeyInfoFactory factory = new DefaultKeyInfoFactory();

            var primary = factory.Create((PrimaryRow x) => new { K11 = x.P11, K12 = x.P12, K13 = x.P13 });
            var foreign = factory.Create((ForeignRow x) => new { K21 = x.P21, K22 = x.P22, K23 = x.P23 });

            // Field mapping (no mirror):
            // P11 - P22
            // P12 - P23
            // P13 - P21
            var converter = RelationKeyConverterFactory.CreateForeignToPrimaryConverter(primary, foreign,
                                                                                        new RelationConstraint <PrimaryRow, ForeignRow, int>(x => x.P11, x => x.P22),
                                                                                        new RelationConstraint <PrimaryRow, ForeignRow, int>(x => x.P12, x => x.P23),
                                                                                        new RelationConstraint <PrimaryRow, ForeignRow, int>(x => (int)x.P13, x => x.P21));

            var result = converter.Invoke(new { K21 = 1, K22 = 2, K23 = 3 });

            Assert.AreEqual(3, result.K11);
            Assert.AreEqual(1, result.K12);
            Assert.AreEqual(2, result.K13);
        }
コード例 #2
0
        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));
        }