예제 #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());
        }
        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);
        }