internal AssociationTypeMapping(
     AssociationType relation,
     AssociationSetMapping associationSetMapping)
 {
     this._associationSetMapping = associationSetMapping;
     this.m_relation             = relation;
 }
        public void Configure_should_rename_table_when_table_configured()
        {
            var database = new EdmModel(DataSpace.SSpace);
            var table = database.AddTable("OriginalName");

            var associationSetMapping
                = new AssociationSetMapping(
                    new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace)),
                    database.GetEntitySet(table))
                    .Initialize();

            var manyToManyAssociationMappingConfiguration
                = new ManyToManyAssociationMappingConfiguration();

            manyToManyAssociationMappingConfiguration.ToTable("NewName");

            var mockPropertyInfo = new MockPropertyInfo();

            associationSetMapping.SourceEndMapping.AssociationEnd = new AssociationEndMember("S", new EntityType("E", "N", DataSpace.CSpace));
            associationSetMapping.SourceEndMapping.AssociationEnd.SetClrPropertyInfo(mockPropertyInfo);

            manyToManyAssociationMappingConfiguration.Configure(associationSetMapping, database, mockPropertyInfo);

            Assert.Equal("NewName", table.GetTableName().Name);
            Assert.Same(manyToManyAssociationMappingConfiguration, table.GetConfiguration());
        }
        public void Configure_should_rename_columns_when_right_keys_configured()
        {
            var database = new EdmModel(DataSpace.CSpace);

            var associationSetMapping
                = new AssociationSetMapping(
                    new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace)),
                    new EntitySet())
                    .Initialize();

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

            associationSetMapping.TargetEndMapping.AddPropertyMapping(new ScalarPropertyMapping(new EdmProperty("PK"), column));

            var manyToManyAssociationMappingConfiguration
                = new ManyToManyAssociationMappingConfiguration();

            manyToManyAssociationMappingConfiguration.MapRightKey("NewName");

            var mockPropertyInfo = new MockPropertyInfo();

            associationSetMapping.SourceEndMapping.AssociationEnd = new AssociationEndMember("S", new EntityType("E", "N", DataSpace.CSpace));
            associationSetMapping.SourceEndMapping.AssociationEnd.SetClrPropertyInfo(mockPropertyInfo);

            manyToManyAssociationMappingConfiguration.Configure(associationSetMapping, database, mockPropertyInfo);

            Assert.Equal("NewName", column.Name);
        }
        /// <summary>
        /// Creates an AssociationTypeMapping instance.
        /// </summary>
        /// <param name="associationSetMapping">The AssociationSetMapping that
        /// the contains this AssociationTypeMapping.</param>
        public AssociationTypeMapping(AssociationSetMapping associationSetMapping)
        {
            Check.NotNull(associationSetMapping, "associationSetMapping");

            _associationSetMapping = associationSetMapping;
            m_relation             = associationSetMapping.AssociationSet.ElementType;
        }
Esempio n. 5
0
        /// <summary>
        /// Removes an association set mapping.
        /// </summary>
        /// <param name="setMapping">The association set mapping to remove.</param>
        public void RemoveSetMapping(AssociationSetMapping setMapping)
        {
            Check.NotNull(setMapping, "setMapping");
            Util.ThrowIfReadOnly(this);

            m_associationSetMappings.Remove(setMapping.Set.Name);
        }
        /// <summary>
        /// Creates an AssociationTypeMapping instance.
        /// </summary>
        /// <param name="associationSetMapping">The AssociationSetMapping that 
        /// the contains this AssociationTypeMapping.</param>
        public AssociationTypeMapping(AssociationSetMapping associationSetMapping)
        {
            Check.NotNull(associationSetMapping, "associationSetMapping");

            _associationSetMapping = associationSetMapping;
            m_relation = associationSetMapping.AssociationSet.ElementType;
        }
        public void Initialize_should_initialize_ends()
        {
            var associationSetMapping 
                = new AssociationSetMapping(new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace)), new EntitySet()).Initialize();

            Assert.NotNull(associationSetMapping.SourceEndMapping);
            Assert.NotNull(associationSetMapping.TargetEndMapping);
        }
        public void Can_get_and_set_configuration_annotation()
        {
            var associationSetMapping 
                = new AssociationSetMapping(new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace)), new EntitySet());

            associationSetMapping.SetConfiguration(42);

            Assert.Equal(42, associationSetMapping.GetConfiguration());
        }
Esempio n. 9
0
        /// <summary>
        /// Adds an association set mapping.
        /// </summary>
        /// <param name="setMapping">The association set mapping to add.</param>
        public void AddSetMapping(AssociationSetMapping setMapping)
        {
            Check.NotNull(setMapping, "setMapping");
            Util.ThrowIfReadOnly(this);

            if (!m_associationSetMappings.ContainsKey(setMapping.Set.Name))
            {
                m_associationSetMappings.Add(setMapping.Set.Name, setMapping);
            }
        }
        public void Can_get_association_set()
        {
            var entitySet = new EntitySet();
            var associationSet = new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace));

            var associationSetMapping
                = new AssociationSetMapping(associationSet, entitySet);

            Assert.Same(associationSet, associationSetMapping.AssociationSet);
        }
 /// <summary>Adds an association set mapping.</summary>
 /// <param name="setMapping">The association set mapping to add.</param>
 public void AddSetMapping(AssociationSetMapping setMapping)
 {
     Check.NotNull <AssociationSetMapping>(setMapping, nameof(setMapping));
     Util.ThrowIfReadOnly((MetadataItem)this);
     if (this.m_associationSetMappings.ContainsKey(setMapping.Set.Name))
     {
         return;
     }
     this.m_associationSetMappings.Add(setMapping.Set.Name, (EntitySetBaseMapping)setMapping);
 }
        public void Can_get_association_set()
        {
            var entitySet      = new EntitySet();
            var associationSet = new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace));

            var associationSetMapping
                = new AssociationSetMapping(associationSet, entitySet);

            Assert.Same(associationSet, associationSetMapping.AssociationSet);
        }
        public void Can_get_association_set_mapping()
        {
            var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace);
            var associationSetMapping
                = new AssociationSetMapping(
                    new AssociationSet("AS", associationType), 
                    new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)));
            var associationTypeMapping = new AssociationTypeMapping(associationSetMapping);

            Assert.Same(associationSetMapping, associationTypeMapping.AssociationSetMapping);
        }
        public void Can_get_table()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var entitySet = new EntitySet("ES", null, null, null, entityType);
            var associationSet = new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace));

            var associationSetMapping
                = new AssociationSetMapping(associationSet, entitySet);

            Assert.Same(entityType, associationSetMapping.Table);
        }
        public void Can_get_association_set_mapping()
        {
            var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace);
            var associationSetMapping
                = new AssociationSetMapping(
                      new AssociationSet("AS", associationType),
                      new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)));
            var associationTypeMapping = new AssociationTypeMapping(associationSetMapping);

            Assert.Same(associationSetMapping, associationTypeMapping.AssociationSetMapping);
        }
        public void IsOfType_collection_empty()
        {
            var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace);

            var setMapping
                = new AssociationSetMapping(
                      new AssociationSet("AS", associationType),
                      new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)));

            Assert.Empty(new AssociationTypeMapping(associationType, setMapping).IsOfTypes);
        }
        public void Can_get_table()
        {
            var entityType     = new EntityType("E", "N", DataSpace.CSpace);
            var entitySet      = new EntitySet("ES", null, null, null, entityType);
            var associationSet = new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace));

            var associationSetMapping
                = new AssociationSetMapping(associationSet, entitySet);

            Assert.Same(entityType, associationSetMapping.Table);
        }
        public void MappingFragments_returns_empty_collection_if_no_mapping_fragments_set()
        {
            var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace);

            var setMapping
                = new AssociationSetMapping(
                      new AssociationSet("AS", associationType),
                      new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)));

            Assert.Empty(new AssociationTypeMapping(associationType, setMapping).MappingFragments);
        }
        public void IsOfType_collection_empty()
        {
            var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace);

            var setMapping
                = new AssociationSetMapping(
                    new AssociationSet("AS", associationType), 
                    new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)));

            Assert.Empty(new AssociationTypeMapping(associationType, setMapping).IsOfTypes);
        }
        public void Association_type_returned_in_type_collection()
        {
            var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace);

            var setMapping
                = new AssociationSetMapping(
                      new AssociationSet("AS", associationType),
                      new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)));

            Assert.Same(
                associationType,
                new AssociationTypeMapping(associationType, setMapping).Types.Single());
        }
        public void Association_type_returned_in_type_collection()
        {
            var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace);

            var setMapping
                = new AssociationSetMapping(
                    new AssociationSet("AS", associationType), 
                    new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)));

            Assert.Same(
                associationType,
                new AssociationTypeMapping(associationType, setMapping).Types.Single());
        }
        public static void GetIdentity_of_StorageSetMapping_returns_expected_value()
        {
            var entityType            = new EntityType("ET", "N", DataSpace.CSpace);
            var entitySet             = new EntitySet("ES", "S", "T", null, entityType);
            var associationType       = new AssociationType("AT", "N", false, DataSpace.CSpace);
            var associationSet        = new AssociationSet("AS", associationType);
            var entitySetMapping      = new EntitySetMapping(entitySet, null);
            var associationSetMapping = new AssociationSetMapping(associationSet, entitySet);

            Assert.Equal(entitySet.Identity,
                         BaseMetadataMappingVisitor.IdentityHelper.GetIdentity(entitySetMapping));
            Assert.Equal(associationSet.Identity,
                         BaseMetadataMappingVisitor.IdentityHelper.GetIdentity(associationSetMapping));
        }
        public void Cannot_set__modification_function_mapping_when_read_only()
        {
            var entitySet      = new EntitySet();
            var associationSet = new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace));
            var associationSetMapping
                = new AssociationSetMapping(associationSet, entitySet);
            var modificationFunctionMapping = new AssociationSetModificationFunctionMapping(associationSet, null, null);

            associationSetMapping.SetReadOnly();

            Assert.Equal(
                Strings.OperationOnReadOnlyItem,
                Assert.Throws <InvalidOperationException>(
                    () => (associationSetMapping.ModificationFunctionMapping = modificationFunctionMapping)).Message);
        }
        public void Cannot_set_target_end_mapping_when_read_only()
        {
            var entitySet      = new EntitySet();
            var associationSet = new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace));
            var associationSetMapping
                = new AssociationSetMapping(associationSet, entitySet);
            var targetEndMapping = new EndPropertyMapping();

            associationSetMapping.SetReadOnly();

            Assert.Equal(
                Strings.OperationOnReadOnlyItem,
                Assert.Throws <InvalidOperationException>(
                    () => (associationSetMapping.TargetEndMapping = targetEndMapping)).Message);
        }
        public void SetReadOnly_is_called_on_child_mapping_items()
        {
            var entityType                  = new EntityType("ET", "N", DataSpace.SSpace);
            var entitySet                   = new EntitySet("ES", "S", "T", "Q", entityType);
            var associationSet              = new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace));
            var associationSetMapping       = new AssociationSetMapping(associationSet, entitySet, null);
            var modificationFunctionMapping = new AssociationSetModificationFunctionMapping(associationSet, null, null);

            associationSetMapping.ModificationFunctionMapping = modificationFunctionMapping;

            Assert.False(associationSetMapping.AssociationTypeMapping.IsReadOnly);
            Assert.False(modificationFunctionMapping.IsReadOnly);
            associationSetMapping.SetReadOnly();
            Assert.True(associationSetMapping.AssociationTypeMapping.IsReadOnly);
            Assert.True(modificationFunctionMapping.IsReadOnly);
        }
        public void Can_get_and_set_store_entity_set()
        {
            var entitySet1 = new EntitySet();
            var associationSet = new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace));

            var associationSetMapping
                = new AssociationSetMapping(associationSet, entitySet1);

            Assert.Same(entitySet1, associationSetMapping.StoreEntitySet);

            var entitySet2 = new EntitySet();

            associationSetMapping.StoreEntitySet = entitySet2;

            Assert.Same(entitySet2, associationSetMapping.StoreEntitySet);
        }
Esempio n. 27
0
        public void Can_get_association_set_mappings()
        {
            var entityContainerMapping = new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace));

            Assert.Empty(entityContainerMapping.AssociationSetMappings);
            Assert.Empty(entityContainerMapping.RelationshipSetMaps);

            var associationSetMapping
                = new AssociationSetMapping(
                      new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace)), entityContainerMapping);

            entityContainerMapping.AddSetMapping(associationSetMapping);

            Assert.Same(associationSetMapping, entityContainerMapping.AssociationSetMappings.Single());
            Assert.Same(associationSetMapping, entityContainerMapping.RelationshipSetMaps.Single());
        }
        public void Can_get_and_set_store_entity_set()
        {
            var entitySet1     = new EntitySet();
            var associationSet = new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace));

            var associationSetMapping
                = new AssociationSetMapping(associationSet, entitySet1);

            Assert.Same(entitySet1, associationSetMapping.StoreEntitySet);

            var entitySet2 = new EntitySet();

            associationSetMapping.StoreEntitySet = entitySet2;

            Assert.Same(entitySet2, associationSetMapping.StoreEntitySet);
        }
        public void Can_get_association_set_mappings()
        {
            var entityContainerMapping = new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace));

            Assert.Empty(entityContainerMapping.AssociationSetMappings);
            Assert.Empty(entityContainerMapping.RelationshipSetMaps);

            var associationSetMapping
                = new AssociationSetMapping(
                    new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace)), entityContainerMapping);

            entityContainerMapping.AddSetMapping(associationSetMapping);

            Assert.Same(associationSetMapping, entityContainerMapping.AssociationSetMappings.Single());
            Assert.Same(associationSetMapping, entityContainerMapping.RelationshipSetMaps.Single());
        }
        public void SetReadOnly_is_called_on_child_mapping_items()
        {
            var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace);
            var associationSet = new AssociationSet("AS", associationType);
            var associationSetMapping
                = new AssociationSetMapping(
                    associationSet,
                    new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)));
            var associationTypeMapping = new AssociationTypeMapping(associationSetMapping);
            var fragment = new MappingFragment(new EntitySet(), associationTypeMapping, false);
            associationTypeMapping.MappingFragment = fragment;

            Assert.False(fragment.IsReadOnly);
            associationTypeMapping.SetReadOnly();
            Assert.True(fragment.IsReadOnly);
        }
        public void MappingFragments_returns_mapping_fragment_if_set()
        {
            var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace);

            var setMapping
                = new AssociationSetMapping(
                      new AssociationSet("AS", associationType),
                      new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)));

            var associationTypeMapping = new AssociationTypeMapping(setMapping);
            var fragment = new MappingFragment(new EntitySet(), associationTypeMapping, false);

            associationTypeMapping.MappingFragment = fragment;

            Assert.Equal(new[] { fragment }, associationTypeMapping.MappingFragments);
            Assert.Same(fragment, associationTypeMapping.MappingFragment);
        }
        public void SetReadOnly_is_called_on_child_mapping_items()
        {
            var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace);
            var associationSet  = new AssociationSet("AS", associationType);
            var associationSetMapping
                = new AssociationSetMapping(
                      associationSet,
                      new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)));
            var associationTypeMapping = new AssociationTypeMapping(associationSetMapping);
            var fragment = new MappingFragment(new EntitySet(), associationTypeMapping, false);

            associationTypeMapping.MappingFragment = fragment;

            Assert.False(fragment.IsReadOnly);
            associationTypeMapping.SetReadOnly();
            Assert.True(fragment.IsReadOnly);
        }
        public void Can_initialize_with_entity_set()
        {
            var entitySet = new EntitySet();
            var associationSet = new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace));

            var associationSetMapping
                = new AssociationSetMapping(associationSet, entitySet);

            var associationTypeMapping = associationSetMapping.TypeMappings.Single();

            Assert.NotNull(associationTypeMapping);
            Assert.Same(associationSet.ElementType, associationTypeMapping.Types.Single());
            Assert.Same(associationSetMapping, associationTypeMapping.SetMapping);

            var mappingFragment = associationTypeMapping.MappingFragments.Single();

            Assert.Same(entitySet, mappingFragment.TableSet);
        }
        public void Can_initialize_with_entity_set()
        {
            var entitySet      = new EntitySet();
            var associationSet = new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace));

            var associationSetMapping
                = new AssociationSetMapping(associationSet, entitySet);

            var associationTypeMapping = associationSetMapping.TypeMappings.Single();

            Assert.NotNull(associationTypeMapping);
            Assert.Same(associationSet.ElementType, associationTypeMapping.Types.Single());
            Assert.Same(associationSetMapping, associationTypeMapping.SetMapping);

            var mappingFragment = associationTypeMapping.MappingFragments.Single();

            Assert.Same(entitySet, mappingFragment.TableSet);
        }
        public void Cannot_add_condition_when_read_only()
        {
            var entitySet      = new EntitySet();
            var associationSet = new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace));
            var associationSetMapping
                = new AssociationSetMapping(associationSet, entitySet);
            var conditionPropertyMapping
                = new ValueConditionMapping(new EdmProperty("C", TypeUsage.Create(new PrimitiveType {
                DataSpace = DataSpace.SSpace
            })), 42);

            associationSetMapping.SetReadOnly();

            Assert.Equal(
                Strings.OperationOnReadOnlyItem,
                Assert.Throws <InvalidOperationException>(
                    () => associationSetMapping.AddCondition(conditionPropertyMapping)).Message);
        }
        public void Configure_should_throw_when_configured_table_not_found()
        {
            var independentAssociationMappingConfiguration
                = new ForeignKeyAssociationMappingConfiguration();

            independentAssociationMappingConfiguration.ToTable("Split");

            var associationSetMapping
                = new AssociationSetMapping(
                    new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace)),
                    new EntitySet())
                    .Initialize();

            var database = new EdmModel(DataSpace.SSpace);

            Assert.Equal(
                Strings.TableNotFound("Split"),
                Assert.Throws<InvalidOperationException>(
                    () => independentAssociationMappingConfiguration
                              .Configure(associationSetMapping, database, new MockPropertyInfo())).
                       Message);
        }
        public void Configure_should_split_key_constraint_when_to_table_configuration()
        {
            var database = new EdmModel(DataSpace.SSpace);
            var sourceTable = database.AddTable("Source");
            var principalTable = database.AddTable("P");

            var fkColumn
                = new EdmProperty(
                    "Fk",
                    ProviderRegistry.Sql2008_ProviderManifest.GetStoreType(
                        TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String))));

            sourceTable.AddColumn(fkColumn);
            var foreignKeyConstraint = new ForeignKeyBuilder(database, "FK")
                                           {
                                               PrincipalTable = principalTable
                                           };
            sourceTable.AddForeignKey(foreignKeyConstraint);
            foreignKeyConstraint.DependentColumns = new[] { fkColumn };
            var targetTable = database.AddTable("Split");
            var associationSetMapping
                = new AssociationSetMapping(
                    new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace)), database.GetEntitySet(sourceTable)).Initialize();
            associationSetMapping.SourceEndMapping.AddPropertyMapping(new ScalarPropertyMapping(new EdmProperty("PK"), fkColumn));

            var independentAssociationMappingConfiguration
                = new ForeignKeyAssociationMappingConfiguration();

            independentAssociationMappingConfiguration.ToTable("Split");

            independentAssociationMappingConfiguration.Configure(associationSetMapping, database, new MockPropertyInfo());

            Assert.True(targetTable.Properties.Contains(fkColumn));
            Assert.True(targetTable.ForeignKeyBuilders.Contains(foreignKeyConstraint));
            Assert.False(sourceTable.Properties.Contains(fkColumn));
            Assert.False(sourceTable.ForeignKeyBuilders.Contains(foreignKeyConstraint));
            Assert.Same(targetTable, associationSetMapping.Table);
        }
Esempio n. 38
0
        public void SetReadOnly_is_called_on_child_mapping_items()
        {
            var conceptualContainer = new EntityContainer("C", DataSpace.CSpace);
            var storeContainer      = new EntityContainer("S", DataSpace.CSpace);
            var containerMapping    = new EntityContainerMapping(conceptualContainer, storeContainer, null, false);

            var entitySet
                = new EntitySet(
                      "ES", "S", "T", "Q",
                      new EntityType("ET", "N", DataSpace.SSpace));
            var entitySetMapping = new EntitySetMapping(entitySet, containerMapping);
            var associationSetMapping
                = new AssociationSetMapping(
                      new AssociationSet(
                          "AS",
                          new AssociationType("AT", "N", false, DataSpace.CSpace)),
                      entitySet);
            var functionImportMapping
                = new FunctionImportMappingFake(
                      new EdmFunction("FI", "N", DataSpace.CSpace),
                      new EdmFunction("TF", "N", DataSpace.SSpace));

            containerMapping.AddSetMapping(entitySetMapping);
            containerMapping.AddSetMapping(associationSetMapping);
            containerMapping.AddFunctionImportMapping(functionImportMapping);

            Assert.False(containerMapping.IsReadOnly);
            Assert.False(entitySetMapping.IsReadOnly);
            Assert.False(associationSetMapping.IsReadOnly);
            Assert.False(functionImportMapping.IsReadOnly);

            containerMapping.SetReadOnly();

            Assert.True(containerMapping.IsReadOnly);
            Assert.True(entitySetMapping.IsReadOnly);
            Assert.True(associationSetMapping.IsReadOnly);
            Assert.True(functionImportMapping.IsReadOnly);
        }
        public void Can_add_get_remove_column_conditions()
        {
            var entitySet1     = new EntitySet();
            var associationSet = new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace));

            var associationSetMapping
                = new AssociationSetMapping(associationSet, entitySet1);

            Assert.Empty(associationSetMapping.Conditions);

            var conditionPropertyMapping
                = new ValueConditionMapping(new EdmProperty("C", TypeUsage.Create(new PrimitiveType {
                DataSpace = DataSpace.SSpace
            })), 42);

            associationSetMapping.AddCondition(conditionPropertyMapping);

            Assert.Same(conditionPropertyMapping, associationSetMapping.Conditions.Single());

            associationSetMapping.RemoveCondition(conditionPropertyMapping);

            Assert.Empty(associationSetMapping.Conditions);
        }
        public void Can_get_and_set_source_and_target_end_mappings()
        {
            var entitySet1 = new EntitySet();
            var associationSet = new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace));

            var associationSetMapping
                = new AssociationSetMapping(associationSet, entitySet1);

            Assert.Null(associationSetMapping.SourceEndMapping);
            Assert.Null(associationSetMapping.TargetEndMapping);

            var sourceEndMapping = new EndPropertyMapping();

            associationSetMapping.SourceEndMapping = sourceEndMapping;

            Assert.Same(sourceEndMapping, associationSetMapping.SourceEndMapping);

            var targetEndMapping = new EndPropertyMapping();

            associationSetMapping.TargetEndMapping = targetEndMapping;

            Assert.Same(targetEndMapping, associationSetMapping.TargetEndMapping);
        }
        public void Can_get_and_set_source_and_target_end_mappings()
        {
            var entitySet1     = new EntitySet();
            var associationSet = new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace));

            var associationSetMapping
                = new AssociationSetMapping(associationSet, entitySet1);

            Assert.Null(associationSetMapping.SourceEndMapping);
            Assert.Null(associationSetMapping.TargetEndMapping);

            var sourceEndMapping = new EndPropertyMapping();

            associationSetMapping.SourceEndMapping = sourceEndMapping;

            Assert.Same(sourceEndMapping, associationSetMapping.SourceEndMapping);

            var targetEndMapping = new EndPropertyMapping();

            associationSetMapping.TargetEndMapping = targetEndMapping;

            Assert.Same(targetEndMapping, associationSetMapping.TargetEndMapping);
        }
 internal abstract void Configure(
     AssociationSetMapping associationSetMapping,
     EdmModel database,
     PropertyInfo navigationProperty);
        private static IEnumerable<Tuple<ModificationFunctionMemberPath, EdmProperty>> GetIndependentFkColumns(
            AssociationSetMapping associationSetMapping)
        {
            DebugCheck.NotNull(associationSetMapping);

            foreach (var propertyMapping in associationSetMapping.SourceEndMapping.PropertyMappings)
            {
                yield return
                    Tuple.Create(
                        new ModificationFunctionMemberPath(
                            new EdmMember[] { propertyMapping.Property, associationSetMapping.SourceEndMapping.AssociationEnd },
                            associationSetMapping.AssociationSet), propertyMapping.Column);
            }

            foreach (var propertyMapping in associationSetMapping.TargetEndMapping.PropertyMappings)
            {
                yield return
                    Tuple.Create(
                        new ModificationFunctionMemberPath(
                            new EdmMember[] { propertyMapping.Property, associationSetMapping.TargetEndMapping.AssociationEnd },
                            associationSetMapping.AssociationSet), propertyMapping.Column);
            }
        }
        public void Generate(AssociationSetMapping associationSetMapping, DbDatabaseMapping databaseMapping)
        {
            DebugCheck.NotNull(associationSetMapping);
            DebugCheck.NotNull(databaseMapping);

            var iaFkProperties = GetIndependentFkColumns(associationSetMapping).ToList();
            var sourceEntityType = associationSetMapping.AssociationSet.ElementType.SourceEnd.GetEntityType();
            var targetEntityType = associationSetMapping.AssociationSet.ElementType.TargetEnd.GetEntityType();
            var functionNamePrefix = sourceEntityType.Name + targetEntityType.Name;

            var insertFunctionMapping
                = GenerateFunctionMapping(
                    ModificationOperator.Insert,
                    associationSetMapping.AssociationSet,
                    associationSetMapping.AssociationSet.ElementType,
                    databaseMapping,
                    Enumerable.Empty<EdmProperty>(),
                    iaFkProperties,
                    new ColumnMappingBuilder[0],
                    functionNamePrefix: functionNamePrefix);

            var deleteFunctionMapping
                = GenerateFunctionMapping(
                    ModificationOperator.Delete,
                    associationSetMapping.AssociationSet,
                    associationSetMapping.AssociationSet.ElementType,
                    databaseMapping,
                    Enumerable.Empty<EdmProperty>(),
                    iaFkProperties,
                    new ColumnMappingBuilder[0],
                    functionNamePrefix: functionNamePrefix);

            associationSetMapping.ModificationFunctionMapping
                = new AssociationSetModificationFunctionMapping(
                    associationSetMapping.AssociationSet,
                    deleteFunctionMapping,
                    insertFunctionMapping);
        }
        public void WriteAssociationSetMapping_should_write_modification_function_mapping()
        {
            var fixture = new Fixture();

            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var entitySet = new EntitySet("ES", "S", null, null, entityType);
            new EntityContainer("EC", DataSpace.SSpace).AddEntitySetBase(entitySet);
            var associationSet = new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace));

            var associationEndMember1 = new AssociationEndMember("Source", new EntityType("E", "N", DataSpace.CSpace));
            associationSet.AddAssociationSetEnd(new AssociationSetEnd(entitySet, associationSet, associationEndMember1));

            var associationEndMember2 = new AssociationEndMember("Target", new EntityType("E", "N", DataSpace.CSpace));
            associationSet.AddAssociationSetEnd(new AssociationSetEnd(entitySet, associationSet, associationEndMember2));

            var storageModificationFunctionMapping
                = new ModificationFunctionMapping(
                    associationSet,
                    associationSet.ElementType,
                    new EdmFunction("F", "N", DataSpace.SSpace, new EdmFunctionPayload()),
                    new[]
                        {
                            new ModificationFunctionParameterBinding(
                                new FunctionParameter(
                                "P",
                                TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)),
                                ParameterMode.In),
                                new ModificationFunctionMemberPath(
                                new EdmMember[]
                                    {
                                        new EdmProperty("K"),
                                        associationEndMember1
                                    },
                                associationSet),
                                true),
                            new ModificationFunctionParameterBinding(
                                new FunctionParameter(
                                "P",
                                TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)),
                                ParameterMode.In),
                                new ModificationFunctionMemberPath(
                                new EdmMember[]
                                    {
                                        new EdmProperty("K"),
                                        associationEndMember2
                                    },
                                associationSet),
                                false)
                        },
                    null,
                    null);

            var associationSetMapping
                = new AssociationSetMapping(
                    associationSet,
                    entitySet)
                      {
                          SourceEndMapping
                              = new EndPropertyMapping
                                    {
                                        AssociationEnd = associationEndMember1
                                    },
                          TargetEndMapping
                              = new EndPropertyMapping
                                    {
                                        AssociationEnd = associationEndMember2
                                    },
                          ModificationFunctionMapping = new AssociationSetModificationFunctionMapping(
                              associationSet,
                              storageModificationFunctionMapping,
                              storageModificationFunctionMapping)
                      };

            fixture.Writer.WriteAssociationSetMappingElement(associationSetMapping);

            Assert.Equal(
                @"<AssociationSetMapping Name=""AS"" TypeName="".A"" StoreEntitySet=""E"">
  <EndProperty Name=""Source"" />
  <EndProperty Name=""Target"" />
  <ModificationFunctionMapping>
    <InsertFunction FunctionName=""N.F"">
      <EndProperty Name=""Source"">
        <ScalarProperty Name=""K"" ParameterName=""P"" Version=""Current"" />
      </EndProperty>
      <EndProperty Name=""Target"">
        <ScalarProperty Name=""K"" ParameterName=""P"" Version=""Original"" />
      </EndProperty>
    </InsertFunction>
    <DeleteFunction FunctionName=""N.F"">
      <EndProperty Name=""Source"">
        <ScalarProperty Name=""K"" ParameterName=""P"" Version=""Current"" />
      </EndProperty>
      <EndProperty Name=""Target"">
        <ScalarProperty Name=""K"" ParameterName=""P"" Version=""Original"" />
      </EndProperty>
    </DeleteFunction>
  </ModificationFunctionMapping>
</AssociationSetMapping>",
                fixture.ToString());
        }
        public void MappingFragments_returns_empty_collection_if_no_mapping_fragments_set()
        {
            var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace);

            var setMapping
                = new AssociationSetMapping(
                    new AssociationSet("AS", associationType), 
                    new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)));

            Assert.Empty(new AssociationTypeMapping(associationType, setMapping).MappingFragments);
        }
        private static AssociationSetMapping CreateIAMapping(EdmModel database, EntityType dependentTable)
        {
            var fkColumn = new EdmProperty(
                "FK", ProviderRegistry.Sql2008_ProviderManifest.GetStoreType(
                    TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String))));

            dependentTable.AddColumn(fkColumn);
            var foreignKeyConstraint = new ForeignKeyBuilder(database, "FK")
            {
                PrincipalTable = database.AddTable("P")
            };

            dependentTable.AddForeignKey(foreignKeyConstraint);
            foreignKeyConstraint.DependentColumns = new[] { fkColumn };

            var associationSetMapping
                = new AssociationSetMapping(
                    new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace)),
                    database.GetEntitySet(dependentTable)).Initialize();

            associationSetMapping.SourceEndMapping.AddPropertyMapping(new ScalarPropertyMapping(new EdmProperty("PK"), fkColumn));
            
            return associationSetMapping;
        }
 /// <summary>Removes an association set mapping.</summary>
 /// <param name="setMapping">The association set mapping to remove.</param>
 public void RemoveSetMapping(AssociationSetMapping setMapping)
 {
     Check.NotNull <AssociationSetMapping>(setMapping, nameof(setMapping));
     Util.ThrowIfReadOnly((MetadataItem)this);
     this.m_associationSetMappings.Remove(setMapping.Set.Name);
 }
        private static AssociationSetMapping BuildAssociationSetMapping(
            CollapsibleEntityAssociationSets collapsibleItem,
            EntityContainerMapping entityContainerMapping,
            SimpleMappingContext mappingContext)
        {
            Debug.Assert(collapsibleItem != null, "collapsibleItem != null");
            Debug.Assert(entityContainerMapping != null, "entityContainerMapping != null");
            Debug.Assert(mappingContext != null, "mappingContext != null");

            var modelAssociationSet = mappingContext[collapsibleItem];
            if (modelAssociationSet.ElementType.IsForeignKey)
            {
                return null;
            }

            var associationSetMapping = new AssociationSetMapping(
                modelAssociationSet, collapsibleItem.EntitySet, entityContainerMapping);

            var count = collapsibleItem.AssociationSets.Count;
            if (count > 0)
            {
                associationSetMapping.SourceEndMapping = BuildEndPropertyMapping(
                    collapsibleItem.GetStoreAssociationSetEnd(0).AssociationSetEnd, mappingContext);
            }
            if (count > 1)
            {
                associationSetMapping.TargetEndMapping = BuildEndPropertyMapping(
                    collapsibleItem.GetStoreAssociationSetEnd(1).AssociationSetEnd, mappingContext);
            }

            return associationSetMapping;
        }
        private static void MoveAssociationSetMappingDependents(
            AssociationSetMapping associationSetMapping,
            EndPropertyMapping dependentMapping,
            EntitySet toSet,
            bool useExistingColumns)
        {
            DebugCheck.NotNull(associationSetMapping);
            DebugCheck.NotNull(dependentMapping);
            DebugCheck.NotNull(toSet);

            var toTable = toSet.ElementType;

            dependentMapping.PropertyMappings.Each(
                pm =>
                    {
                        var oldColumn = pm.Column;

                        pm.Column
                            = TableOperations.MoveColumnAndAnyConstraints(
                                associationSetMapping.Table, toTable, oldColumn, useExistingColumns);

                        associationSetMapping.Conditions
                                             .Where(cc => cc.Column == oldColumn)
                                             .Each(cc => cc.Column = pm.Column);
                    });

            associationSetMapping.StoreEntitySet = toSet;
        }
        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 void GenerateIndependentForeignKeyConstraint(
            DbDatabaseMapping databaseMapping,
            EntityType principalEntityType,
            EntityType dependentEntityType,
            EntityType dependentTable,
            AssociationSetMapping associationSetMapping,
            EndPropertyMapping associationEndMapping,
            string name,
            AssociationEndMember principalEnd,
            bool isPrimaryKeyColumn = false)
        {
            DebugCheck.NotNull(databaseMapping);
            DebugCheck.NotNull(principalEntityType);
            DebugCheck.NotNull(dependentTable);
            DebugCheck.NotNull(associationEndMapping);
            DebugCheck.NotEmpty(name);

            var principalTable
                = GetEntityTypeMappingInHierarchy(databaseMapping, principalEntityType)
                    .MappingFragments
                    .Single()
                    .Table;

            var foreignKeyConstraint
                = new ForeignKeyBuilder(databaseMapping.Database, name)
                    {
                        PrincipalTable = principalTable,
                        DeleteAction = associationEndMapping.AssociationEnd.DeleteBehavior != OperationAction.None
                                           ? associationEndMapping.AssociationEnd.DeleteBehavior
                                           : OperationAction.None
                    };

            var principalNavigationProperty
                = databaseMapping.Model.EntityTypes
                                 .SelectMany(e => e.DeclaredNavigationProperties)
                                 .SingleOrDefault(n => n.ResultEnd == principalEnd);

            dependentTable.AddForeignKey(foreignKeyConstraint);

            foreignKeyConstraint.DependentColumns = GenerateIndependentForeignKeyColumns(
                principalEntityType,
                dependentEntityType,
                associationSetMapping,
                associationEndMapping,
                dependentTable,
                isPrimaryKeyColumn,
                principalNavigationProperty);
        }
        public void MappingFragments_returns_mapping_fragment_if_set()
        {
            var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace);

            var setMapping
                = new AssociationSetMapping(
                    new AssociationSet("AS", associationType),
                    new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)));

            var associationTypeMapping = new AssociationTypeMapping(setMapping);
            var fragment = new MappingFragment(new EntitySet(), associationTypeMapping, false);
            associationTypeMapping.MappingFragment = fragment;

            Assert.Equal(new[] { fragment }, associationTypeMapping.MappingFragments);
            Assert.Same(fragment, associationTypeMapping.MappingFragment);
        }
 // <summary>
 // Construct the new AssociationTypeMapping object.
 // </summary>
 // <param name="relation"> Represents the Association Type metadata object </param>
 // <param name="associationSetMapping"> Set Mapping that contains this Type mapping </param>
 internal AssociationTypeMapping(AssociationType relation, AssociationSetMapping associationSetMapping)
 {
     _associationSetMapping = associationSetMapping;
     m_relation = relation;
 }
        internal override void Configure(
            AssociationSetMapping associationSetMapping, EdmModel database, PropertyInfo navigationProperty)
        {
            DebugCheck.NotNull(associationSetMapping);

            DebugCheck.NotNull(navigationProperty);

            // By convention source end contains the dependent column mappings
            var propertyMappings = associationSetMapping.SourceEndMapping.PropertyMappings.ToList();

            if (_tableName != null)
            {
                var targetTable
                    = ((from t in database.EntityTypes
                        let n = t.GetTableName()
                        where (n != null && n.Equals(_tableName))
                        select t)
                          .SingleOrDefault())
                      ?? (from es in database.GetEntitySets()
                          where string.Equals(es.Table, _tableName.Name, StringComparison.Ordinal)
                          select es.ElementType).SingleOrDefault();

                if (targetTable == null)
                {
                    throw Error.TableNotFound(_tableName);
                }

                var sourceTable = associationSetMapping.Table;

                if (sourceTable != targetTable)
                {
                    var foreignKeyConstraint
                        = sourceTable.ForeignKeyBuilders
                                     .Single(fk => fk.DependentColumns.SequenceEqual(propertyMappings.Select(pm => pm.Column)));

                    sourceTable.RemoveForeignKey(foreignKeyConstraint);
                    targetTable.AddForeignKey(foreignKeyConstraint);

                    foreignKeyConstraint.DependentColumns
                                        .Each(
                                            c =>
                                            {
                                                var isKey = c.IsPrimaryKeyColumn;

                                                sourceTable.RemoveMember(c);
                                                targetTable.AddMember(c);

                                                if (isKey)
                                                {
                                                    targetTable.AddKeyMember(c);
                                                }
                                            });

                    associationSetMapping.StoreEntitySet = database.GetEntitySet(targetTable);
                }
            }

            if ((_keyColumnNames.Count > 0)
                && (_keyColumnNames.Count != propertyMappings.Count()))
            {
                throw Error.IncorrectColumnCount(string.Join(", ", _keyColumnNames));
            }

            _keyColumnNames.Each((n, i) => propertyMappings[i].Column.Name = n);

            foreach (var annotation in _annotations)
            {
                var index = _keyColumnNames.IndexOf(annotation.Key.Item1);

                if (index == -1)
                {
                    throw new InvalidOperationException(Strings.BadKeyNameForAnnotation(annotation.Key.Item1, annotation.Key.Item2));
                }

                propertyMappings[index].Column.AddAnnotation(
                    XmlConstants.CustomAnnotationPrefix + annotation.Key.Item2,
                    annotation.Value);
            }
        }
 /// <summary>Creates an AssociationTypeMapping instance.</summary>
 /// <param name="associationSetMapping">The AssociationSetMapping that
 /// the contains this AssociationTypeMapping.</param>
 public AssociationTypeMapping(AssociationSetMapping associationSetMapping)
 {
     Check.NotNull <AssociationSetMapping>(associationSetMapping, nameof(associationSetMapping));
     this._associationSetMapping = associationSetMapping;
     this.m_relation             = associationSetMapping.AssociationSet.ElementType;
 }