public void WriteFunctionMapping_should_write_association_end_bindings()
        {
            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 StorageModificationFunctionMapping(
                      entitySet,
                      entityType,
                      new EdmFunction("F", "N", DataSpace.SSpace, new EdmFunctionPayload()),
                      new[]
            {
                new StorageModificationFunctionParameterBinding(
                    new FunctionParameter(
                        "P",
                        TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)),
                        ParameterMode.In),
                    new StorageModificationFunctionMemberPath(
                        new EdmMember[]
                {
                    new EdmProperty("K"),
                    associationEndMember1
                },
                        associationSet),
                    true)
            },
                      null,
                      null);

            fixture.Writer.WriteFunctionMapping("InsertFunction", storageModificationFunctionMapping);

            Assert.Equal(
                @"<InsertFunction FunctionName=""N.F"">
  <AssociationEnd AssociationSet=""AS"" From=""Source"" To=""Target"">
    <ScalarProperty Name=""K"" ParameterName=""P"" Version=""Current"" />
  </AssociationEnd>
</InsertFunction>",
                fixture.ToString());
        }
            private static Mock <RelatedEnd> CreateMockRelatedEnd()
            {
                var relatedEndMock = new Mock <RelatedEnd>
                {
                    CallBase = true
                };

                var associationType = new AssociationType(
                    name: "associationName",
                    namespaceName: "associationNamespace",
                    foreignKey: true,
                    dataSpace: DataSpace.CSpace);
                var associationSet = new AssociationSet(name: "associationSetName", associationType: associationType);

                var refType =
                    new RefType(new EntityType(name: "entityTypeName", namespaceName: "entityTypeNamespace", dataSpace: DataSpace.CSpace));
                var toEndMember = new AssociationEndMember(
                    name: "toEndMember",
                    endRefType: refType,
                    multiplicity: RelationshipMultiplicity.Many);

                associationSet.AddAssociationSetEnd(new AssociationSetEnd(new EntitySet(), associationSet, toEndMember));
                relatedEndMock.Setup(m => m.RelationshipSet).Returns(associationSet);
                relatedEndMock.Setup(m => m.TargetRoleName).Returns("toEndMember");

                relatedEndMock.Setup(m => m.EntityWrapperFactory).Returns(CreateMockEntityWrapperFactory().Object);

                return(relatedEndMock);
            }
Exemple #3
0
        protected override void InvokeInternal(CommandProcessorContext cpc)
        {
            Debug.Assert(Association != null, "InvokeInternal is called when Association is null.");
            if (Association == null)
            {
                throw new InvalidOperationException("InvokeInternal is called when Association is null");
            }

            var service  = cpc.EditingContext.GetEFArtifactService();
            var artifact = service.Artifact;

            // the entity container we want to add it to
            BaseEntityContainer entityContainer = null;

            switch (CommandModelSpace)
            {
            case ModelSpace.Conceptual:
                entityContainer = artifact.ConceptualModel().FirstEntityContainer;
                break;

            case ModelSpace.Storage:
                entityContainer = artifact.StorageModel().FirstEntityContainer;
                break;
            }
            Debug.Assert(entityContainer != null, "No entity container");

            // check for uniqueness
            string msg = null;

            if (ModelHelper.IsUniqueName(typeof(AssociationSet), entityContainer, Name, false, out msg) == false)
            {
                throw new InvalidOperationException(msg);
            }

            // create the new item in our model
            var associationSet = new AssociationSet(entityContainer, null);

            associationSet.LocalName.Value = Name;
            entityContainer.AddAssociationSet(associationSet);

            // set the association binding: needs to happen before the ends are resolved
            if (Association != null)
            {
                associationSet.Association.SetRefName(Association);
            }

            // TODO: what should we create if these bindings are unknown?
            var end1 = Association.AssociationEnds()[0];
            var end2 = Association.AssociationEnds()[1];

            if (end1 != null &&
                end1.Type.Status == BindingStatus.Known &&
                end2 != null &&
                end2.Type.Status == BindingStatus.Known)
            {
                var setEnd1 = new AssociationSetEnd(associationSet, null);
                setEnd1.Role.SetRefName(end1);
                setEnd1.EntitySet.SetRefName(end1.Type.Target.EntitySet);
                associationSet.AddAssociationSetEnd(setEnd1);

                var setEnd2 = new AssociationSetEnd(associationSet, null);
                setEnd2.Role.SetRefName(end2);
                setEnd2.EntitySet.SetRefName(end2.Type.Target.EntitySet);
                associationSet.AddAssociationSetEnd(setEnd2);
            }

            XmlModelHelper.NormalizeAndResolve(associationSet);

            _createdAssociationSet = associationSet;
        }
        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 StorageModificationFunctionMapping(
                      associationSet,
                      associationSet.ElementType,
                      new EdmFunction("F", "N", DataSpace.SSpace, new EdmFunctionPayload()),
                      new[]
            {
                new StorageModificationFunctionParameterBinding(
                    new FunctionParameter(
                        "P",
                        TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)),
                        ParameterMode.In),
                    new StorageModificationFunctionMemberPath(
                        new EdmMember[]
                {
                    new EdmProperty("K"),
                    associationEndMember1
                },
                        associationSet),
                    true),
                new StorageModificationFunctionParameterBinding(
                    new FunctionParameter(
                        "P",
                        TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)),
                        ParameterMode.In),
                    new StorageModificationFunctionMemberPath(
                        new EdmMember[]
                {
                    new EdmProperty("K"),
                    associationEndMember2
                },
                        associationSet),
                    false)
            },
                      null,
                      null);

            var associationSetMapping
                = new StorageAssociationSetMapping(
                      associationSet,
                      entitySet)
                {
                SourceEndMapping
                    = new StorageEndPropertyMapping
                    {
                    EndMember = associationEndMember1
                    },
                TargetEndMapping
                    = new StorageEndPropertyMapping
                    {
                    EndMember = associationEndMember2
                    },
                ModificationFunctionMapping = new StorageAssociationSetModificationFunctionMapping(
                    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 Can_configure_ia_fk_parameters()
        {
            var modificationFunctionConfiguration = new ModificationFunctionConfiguration();

            var mockPropertyInfo1 = new MockPropertyInfo();
            var mockPropertyInfo2 = new MockPropertyInfo();

            modificationFunctionConfiguration
            .Parameter(new PropertyPath(new[] { mockPropertyInfo1.Object, mockPropertyInfo2.Object }), "Foo");

            var entitySet = new EntitySet();

            entitySet.ChangeEntityContainerWithoutCollectionFixup(new EntityContainer("C", DataSpace.CSpace));

            var property1 = new EdmProperty("P1");

            property1.SetClrPropertyInfo(mockPropertyInfo1);

            var function           = new EdmFunction("F", "N", DataSpace.SSpace);
            var functionParameter1 = new FunctionParameter();

            var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace);

            var associationEndMember1
                = new AssociationEndMember("AE1", new EntityType("E", "N", DataSpace.CSpace))
                {
                RelationshipMultiplicity = RelationshipMultiplicity.Many
                };

            var associationEndMember2
                = new AssociationEndMember("AE2", new EntityType("E", "N", DataSpace.CSpace))
                {
                RelationshipMultiplicity = RelationshipMultiplicity.One
                };

            associationEndMember2.SetClrPropertyInfo(mockPropertyInfo1);

            associationType.SourceEnd = associationEndMember1;
            associationType.TargetEnd = associationEndMember2;

            var associationSet = new AssociationSet("AS", associationType);

            associationSet.AddAssociationSetEnd(
                new AssociationSetEnd(entitySet, associationSet, associationEndMember2));

            modificationFunctionConfiguration.Configure(
                new StorageModificationFunctionMapping(
                    entitySet,
                    new EntityType("E", "N", DataSpace.CSpace),
                    function,
                    new[]
            {
                new StorageModificationFunctionParameterBinding(
                    functionParameter1,
                    new StorageModificationFunctionMemberPath(
                        new EdmMember[] { property1, associationEndMember2 },
                        associationSet),
                    false)
            },
                    null,
                    null),
                ProviderRegistry.Sql2008_ProviderManifest);

            Assert.Equal("Foo", functionParameter1.Name);
        }
        public void Can_configure_function_name_and_parameters()
        {
            var modificationFunctionConfiguration = new ModificationFunctionConfiguration();

            modificationFunctionConfiguration.HasName("Foo", "Bar");

            var mockPropertyInfo1 = new MockPropertyInfo();
            var mockPropertyInfo2 = new MockPropertyInfo();

            modificationFunctionConfiguration
            .Parameter(new PropertyPath(mockPropertyInfo1), "Foo");

            modificationFunctionConfiguration
            .Parameter(new PropertyPath(new[] { mockPropertyInfo1.Object, mockPropertyInfo2.Object }), "Bar");

            var entitySet = new EntitySet();

            entitySet.ChangeEntityContainerWithoutCollectionFixup(new EntityContainer("C", DataSpace.CSpace));

            var property1 = new EdmProperty("P1");

            property1.SetClrPropertyInfo(mockPropertyInfo1);

            var property2 = new EdmProperty("P1");

            property2.SetClrPropertyInfo(mockPropertyInfo2);

            var functionParameter1
                = new FunctionParameter(
                      "P1",
                      TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)),
                      ParameterMode.In);

            var functionParameter2
                = new FunctionParameter(
                      "P2",
                      TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)),
                      ParameterMode.In);

            var functionParameter3
                = new FunctionParameter(
                      "Foo",
                      TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)),
                      ParameterMode.In);

            var function
                = new EdmFunction(
                      "F", "N", DataSpace.SSpace,
                      new EdmFunctionPayload
            {
                Parameters = new[] { functionParameter1, functionParameter2, functionParameter3 }
            });

            var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace);

            var associationEndMember1
                = new AssociationEndMember("AE1", new EntityType("E", "N", DataSpace.CSpace))
                {
                RelationshipMultiplicity = RelationshipMultiplicity.Many
                };

            var associationEndMember2
                = new AssociationEndMember("AE2", new EntityType("E", "N", DataSpace.CSpace))
                {
                RelationshipMultiplicity = RelationshipMultiplicity.Many
                };

            associationType.SourceEnd = associationEndMember1;
            associationType.TargetEnd = associationEndMember2;

            var associationSet = new AssociationSet("AS", associationType);

            associationSet.AddAssociationSetEnd(
                new AssociationSetEnd(entitySet, associationSet, associationEndMember2));

            modificationFunctionConfiguration.Configure(
                new StorageModificationFunctionMapping(
                    entitySet,
                    new EntityType("E", "N", DataSpace.CSpace),
                    function,
                    new[]
            {
                new StorageModificationFunctionParameterBinding(
                    functionParameter1,
                    new StorageModificationFunctionMemberPath(
                        new EdmMember[] { property1, associationEndMember2 },
                        associationSet),
                    false),
                new StorageModificationFunctionParameterBinding(
                    functionParameter2,
                    new StorageModificationFunctionMemberPath(
                        new[] { property1, property2 },
                        null),
                    false)
            },
                    null,
                    null),
                ProviderRegistry.Sql2008_ProviderManifest);

            Assert.Equal("Foo", function.StoreFunctionNameAttribute);
            Assert.Equal("Bar", function.Schema);
            Assert.Equal("Foo", functionParameter1.Name);
            Assert.Equal("Bar", functionParameter2.Name);
            Assert.Equal("Foo1", functionParameter3.Name);
        }