public void Can_get_property_mappings()
        {
            var endPropertyMapping = new EndPropertyMapping();

            Assert.Empty(endPropertyMapping.PropertyMappings);

            var scalarPropertyMapping
                = new ScalarPropertyMapping(new EdmProperty("P"), new EdmProperty("C", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })));

            endPropertyMapping.AddPropertyMapping(scalarPropertyMapping);

            Assert.Same(scalarPropertyMapping, endPropertyMapping.PropertyMappings.Single());
        }
        public void Cannot_add_property_when_read_only()
        {
            var associationEnd = new AssociationEndMember("E", new EntityType("E", "N", DataSpace.CSpace));
            var mapping = new EndPropertyMapping(associationEnd);
            mapping.SetReadOnly();

            Assert.True(mapping.IsReadOnly);

            var scalarPropertyMapping
                = new ScalarPropertyMapping(new EdmProperty("P"), new EdmProperty("C", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })));

            Assert.Equal(
                Strings.OperationOnReadOnlyItem,
                Assert.Throws<InvalidOperationException>(
                    () => mapping.AddPropertyMapping(scalarPropertyMapping)).Message);
        }
        public void Can_get_property_mappings()
        {
            var endPropertyMapping = new EndPropertyMapping();

            Assert.Empty(endPropertyMapping.PropertyMappings);

            var scalarPropertyMapping
                = new ScalarPropertyMapping(new EdmProperty("P"), new EdmProperty("C", TypeUsage.Create(new PrimitiveType()
            {
                DataSpace = DataSpace.SSpace
            })));

            endPropertyMapping.AddPropertyMapping(scalarPropertyMapping);

            Assert.Same(scalarPropertyMapping, endPropertyMapping.PropertyMappings.Single());
        }
        public void SetReadOnly_is_called_on_child_mapping_items()
        {
            var associationEnd = new AssociationEndMember("E", new EntityType("E", "N", DataSpace.CSpace));
            var mapping        = new EndPropertyMapping(associationEnd);
            var scalarPropertyMapping
                = new ScalarPropertyMapping(new EdmProperty("P"), new EdmProperty("C", TypeUsage.Create(new PrimitiveType()
            {
                DataSpace = DataSpace.SSpace
            })));

            mapping.AddPropertyMapping(scalarPropertyMapping);

            Assert.False(scalarPropertyMapping.IsReadOnly);
            mapping.SetReadOnly();
            Assert.True(scalarPropertyMapping.IsReadOnly);
        }
        public void Cannot_remove_property_when_read_only()
        {
            var associationEnd = new AssociationEndMember("E", new EntityType("E", "N", DataSpace.CSpace));
            var mapping        = new EndPropertyMapping(associationEnd);
            var scalarPropertyMapping
                = new ScalarPropertyMapping(new EdmProperty("P"), new EdmProperty("C", TypeUsage.Create(new PrimitiveType()
            {
                DataSpace = DataSpace.SSpace
            })));

            mapping.AddPropertyMapping(scalarPropertyMapping);
            mapping.SetReadOnly();

            Assert.True(mapping.IsReadOnly);

            Assert.Equal(
                Strings.OperationOnReadOnlyItem,
                Assert.Throws <InvalidOperationException>(
                    () => mapping.RemovePropertyMapping(scalarPropertyMapping)).Message);
        }
        private IEnumerable<EdmProperty> GenerateIndependentForeignKeyColumns(
            EntityType principalEntityType,
            EntityType dependentEntityType,
            AssociationSetMapping associationSetMapping,
            EndPropertyMapping associationEndMapping,
            EntityType dependentTable,
            bool isPrimaryKeyColumn,
            NavigationProperty principalNavigationProperty)
        {
            DebugCheck.NotNull(principalEntityType);
            DebugCheck.NotNull(associationEndMapping);
            DebugCheck.NotNull(dependentTable);

            foreach (var property in principalEntityType.KeyProperties())
            {
                var columnName
                    = ((principalNavigationProperty != null)
                           ? principalNavigationProperty.Name
                           : principalEntityType.Name) + "_" + property.Name;

                var foreignKeyColumn
                    = MapTableColumn(property, columnName, false);

                dependentTable.AddColumn(foreignKeyColumn);

                if (isPrimaryKeyColumn)
                {
                    dependentTable.AddKeyMember(foreignKeyColumn);
                }

                foreignKeyColumn.Nullable
                    = associationEndMapping.AssociationEnd.IsOptional()
                      || (associationEndMapping.AssociationEnd.IsRequired()
                          && dependentEntityType.BaseType != null);

                foreignKeyColumn.StoreGeneratedPattern = StoreGeneratedPattern.None;

                yield return foreignKeyColumn;

                associationEndMapping.AddPropertyMapping(new ScalarPropertyMapping(property, foreignKeyColumn));

                if (foreignKeyColumn.Nullable)
                {
                    associationSetMapping
                        .AddCondition(new IsNullConditionMapping(foreignKeyColumn, false));
                }
            }
        }
        private static EndPropertyMapping BuildEndPropertyMapping(
            AssociationSetEnd storeSetEnd,
            SimpleMappingContext mappingContext)
        {
            Debug.Assert(storeSetEnd != null, "storeSetEnd != null");
            Debug.Assert(mappingContext != null, "mappingContext != null");

            var endPropertyMapping =
                new EndPropertyMapping
                    {
                        AssociationEnd = mappingContext[storeSetEnd].CorrespondingAssociationEndMember
                    };

            foreach (EdmProperty storeKeyMember in storeSetEnd.EntitySet.ElementType.KeyMembers)
            {
                var modelKeyMember = mappingContext[storeKeyMember];
                var storeFkTableMember = GetAssociatedFkColumn(storeSetEnd, storeKeyMember);

                endPropertyMapping.AddPropertyMapping(
                    new ScalarPropertyMapping(modelKeyMember, storeFkTableMember));
            }

            return endPropertyMapping;
        }
        public void SetReadOnly_is_called_on_child_mapping_items()
        {
            var associationEnd = new AssociationEndMember("E", new EntityType("E", "N", DataSpace.CSpace));
            var mapping = new EndPropertyMapping(associationEnd);
            var scalarPropertyMapping
                = new ScalarPropertyMapping(new EdmProperty("P"), new EdmProperty("C", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })));
            mapping.AddPropertyMapping(scalarPropertyMapping);

            Assert.False(scalarPropertyMapping.IsReadOnly);
            mapping.SetReadOnly();
            Assert.True(scalarPropertyMapping.IsReadOnly);
        }