コード例 #1
0
        public void Map(
            PropertyInfo propertyInfo,
            EntityType entityType,
            Func <EntityTypeConfiguration> entityTypeConfiguration)
        {
            Type elementType = propertyInfo.PropertyType;
            RelationshipMultiplicity relationshipMultiplicity = RelationshipMultiplicity.ZeroOrOne;

            if (elementType.IsCollection(out elementType))
            {
                relationshipMultiplicity = RelationshipMultiplicity.Many;
            }
            EntityType targetEntityType = this._typeMapper.MapEntityType(elementType);

            if (targetEntityType == null)
            {
                return;
            }
            RelationshipMultiplicity sourceAssociationEndKind = relationshipMultiplicity.IsMany() ? RelationshipMultiplicity.ZeroOrOne : RelationshipMultiplicity.Many;
            AssociationType          associationType          = this._typeMapper.MappingContext.Model.AddAssociationType(entityType.Name + "_" + propertyInfo.Name, entityType, sourceAssociationEndKind, targetEntityType, relationshipMultiplicity, this._typeMapper.MappingContext.ModelConfiguration.ModelNamespace);

            associationType.SourceEnd.SetClrPropertyInfo(propertyInfo);
            this._typeMapper.MappingContext.Model.AddAssociationSet(associationType.Name, associationType);
            NavigationProperty property = entityType.AddNavigationProperty(propertyInfo.Name, associationType);

            property.SetClrPropertyInfo(propertyInfo);
            this._typeMapper.MappingContext.ConventionsConfiguration.ApplyPropertyConfiguration(propertyInfo, (Func <PropertyConfiguration>)(() => (PropertyConfiguration)entityTypeConfiguration().Navigation(propertyInfo)), this._typeMapper.MappingContext.ModelConfiguration);
            new AttributeMapper(this._typeMapper.MappingContext.AttributeProvider).Map(propertyInfo, (ICollection <MetadataProperty>)property.GetMetadataProperties());
        }
コード例 #2
0
        public void Map(
            PropertyInfo propertyInfo, EntityType entityType, Func <EntityTypeConfiguration> entityTypeConfiguration)
        {
            DebugCheck.NotNull(propertyInfo);
            DebugCheck.NotNull(entityType);
            DebugCheck.NotNull(entityTypeConfiguration);

            var targetType = propertyInfo.PropertyType;
            var targetAssociationEndKind = RelationshipMultiplicity.ZeroOrOne;

            if (targetType.IsCollection(out targetType))
            {
                targetAssociationEndKind = RelationshipMultiplicity.Many;
            }

            var targetEntityType = _typeMapper.MapEntityType(targetType);

            if (targetEntityType != null)
            {
                var sourceAssociationEndKind
                    = targetAssociationEndKind.IsMany()
                          ? RelationshipMultiplicity.ZeroOrOne
                          : RelationshipMultiplicity.Many;

                var associationType
                    = _typeMapper.MappingContext.Model.AddAssociationType(
                          entityType.Name + "_" + propertyInfo.Name,
                          entityType,
                          sourceAssociationEndKind,
                          targetEntityType,
                          targetAssociationEndKind,
                          _typeMapper.MappingContext.ModelConfiguration.ModelNamespace);

                associationType.SourceEnd.SetClrPropertyInfo(propertyInfo);

                _typeMapper.MappingContext.Model.AddAssociationSet(associationType.Name, associationType);

                var navigationProperty
                    = entityType.AddNavigationProperty(propertyInfo.Name, associationType);

                navigationProperty.SetClrPropertyInfo(propertyInfo);

                _typeMapper.MappingContext.ConventionsConfiguration.ApplyPropertyConfiguration(
                    propertyInfo,
                    () => entityTypeConfiguration().Navigation(propertyInfo),
                    _typeMapper.MappingContext.ModelConfiguration);

                new AttributeMapper(_typeMapper.MappingContext.AttributeProvider)
                .Map(propertyInfo, navigationProperty.Annotations);
            }
        }
コード例 #3
0
        public void Configure_should_ensure_consistency_of_principality_when_already_configured()
        {
            var associationType  = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace);
            var sourceEntityType = new EntityType("SE", "N", DataSpace.CSpace);

            associationType.SourceEnd = new AssociationEndMember("S", sourceEntityType);
            var targetEntityType = new EntityType("TE", "N", DataSpace.CSpace);

            associationType.TargetEnd = new AssociationEndMember("T", targetEntityType);

            var navPropertyInfoA = new MockPropertyInfo(typeof(AType1), "N1");
            var navigationPropertyConfigurationA = new NavigationPropertyConfiguration(navPropertyInfoA);

            associationType.SetConfiguration(navigationPropertyConfigurationA);
            var navPropertyA = new NavigationProperty("N1", TypeUsage.Create(targetEntityType));

            navPropertyA.SetClrPropertyInfo(navPropertyInfoA);
            navPropertyA.RelationshipType = associationType;
            navPropertyA.ToEndMember      = associationType.TargetEnd;
            navPropertyA.FromEndMember    = associationType.SourceEnd;
            sourceEntityType.AddNavigationProperty(navPropertyA);

            var navPropertyInfoB = new MockPropertyInfo(typeof(AType1), "N2");
            var navigationPropertyConfigurationB
                = new NavigationPropertyConfiguration(navPropertyInfoB)
                {
                IsNavigationPropertyDeclaringTypePrincipal = false
                };
            var navPropertyB = new NavigationProperty("N2", TypeUsage.Create(sourceEntityType));

            navPropertyB.SetClrPropertyInfo(navPropertyInfoB);
            navPropertyB.RelationshipType = associationType;
            navPropertyB.ToEndMember      = associationType.SourceEnd;
            navPropertyB.FromEndMember    = associationType.TargetEnd;
            targetEntityType.AddNavigationProperty(navPropertyB);

            var model = new EdmModel(DataSpace.CSpace);

            model.AddItem(sourceEntityType);
            model.AddItem(targetEntityType);

            navigationPropertyConfigurationB.Configure(
                new NavigationProperty("N", TypeUsage.Create(associationType.TargetEnd.GetEntityType()))
            {
                RelationshipType = associationType
            },
                model, new EntityTypeConfiguration(typeof(object)));

            Assert.Equal(true, navigationPropertyConfigurationA.IsNavigationPropertyDeclaringTypePrincipal);
        }
コード例 #4
0
        public void GetNavigationProperty_should_return_correct_property()
        {
            var entityType      = new EntityType("E", "N", DataSpace.CSpace);
            var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace);

            associationType.SourceEnd = new AssociationEndMember("S", new EntityType("E", "N", DataSpace.CSpace));
            associationType.TargetEnd = new AssociationEndMember("T", new EntityType("E", "N", DataSpace.CSpace));
            var property = entityType.AddNavigationProperty("Foo", associationType);

            var foundProperty = entityType.NavigationProperties.SingleOrDefault(np => np.Name == "Foo");

            Assert.NotNull(foundProperty);
            Assert.Same(property, foundProperty);
        }
コード例 #5
0
        public void GetNavigationProperty_should_return_correct_property()
        {
            var entityType      = new EntityType();
            var associationType = new AssociationType();

            associationType.SourceEnd = new AssociationEndMember("S", new EntityType());
            associationType.TargetEnd = new AssociationEndMember("T", new EntityType());
            var property = entityType.AddNavigationProperty("Foo", associationType);

            var foundProperty = entityType.NavigationProperties.SingleOrDefault(np => np.Name == "Foo");

            Assert.NotNull(foundProperty);
            Assert.Same(property, foundProperty);
        }
コード例 #6
0
        public void AddNavigationProperty_should_create_and_add_navigation_property()
        {
            var entityType      = new EntityType();
            var associationType = new AssociationType();

            associationType.SourceEnd = new AssociationEndMember("S", new EntityType());
            associationType.TargetEnd = new AssociationEndMember("T", new EntityType());

            var navigationProperty = entityType.AddNavigationProperty("N", associationType);

            Assert.NotNull(navigationProperty);
            Assert.Equal("N", navigationProperty.Name);
            Assert.Same(associationType, navigationProperty.Association);
            Assert.True(entityType.NavigationProperties.Contains(navigationProperty));
        }
コード例 #7
0
        public void AddNavigationProperty_should_create_and_add_navigation_property()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var associationType
                = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace)
                {
                SourceEnd = new AssociationEndMember("S", new EntityType("E", "N", DataSpace.CSpace)),
                TargetEnd = new AssociationEndMember("T", new EntityType("E", "N", DataSpace.CSpace).GetReferenceType(), RelationshipMultiplicity.Many)
                };

            var navigationProperty = entityType.AddNavigationProperty("N", associationType);

            Assert.NotNull(navigationProperty);
            Assert.Equal("N", navigationProperty.Name);
            Assert.Same(associationType, navigationProperty.Association);
            Assert.Equal(BuiltInTypeKind.CollectionType, navigationProperty.TypeUsage.EdmType.BuiltInTypeKind);
            Assert.Same(associationType.SourceEnd, navigationProperty.FromEndMember);
            Assert.Same(associationType.TargetEnd, navigationProperty.ToEndMember);
            Assert.True(entityType.NavigationProperties.Contains(navigationProperty));
        }
コード例 #8
0
        private static EdmModel CreateModelFixture(
            out EntityType declaringEntityType, out EntityType complexEntityType)
        {
            var model = new EdmModel().Initialize();

            declaringEntityType = model.AddEntityType("E");
            complexEntityType   = model.AddEntityType("C");
            var property = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            complexEntityType.AddMember(property);

            var associationType
                = model.AddAssociationType(
                      "A",
                      declaringEntityType, RelationshipMultiplicity.Many,
                      complexEntityType, RelationshipMultiplicity.ZeroOrOne);

            declaringEntityType.AddNavigationProperty("E.C", associationType);

            return(model);
        }