Exemplo n.º 1
0
        public TestModelBuilder Association(
            string sourceEntity, RelationshipMultiplicity sourceEndKind,
            string targetEntity, RelationshipMultiplicity targetEndKind)
        {
            _model.AddAssociationSet(
                "AssociationSet" + _counter++,
                _model.AddAssociationType(
                    "Association",
                    _model.GetEntityType(sourceEntity), sourceEndKind,
                    _model.GetEntityType(targetEntity), targetEndKind));

            return(this);
        }
        public void Generate_should_not_generate_modification_function_mappings_when_ends_not_mapped_to_functions()
        {
            var model = new EdmModel(DataSpace.CSpace);

            var entityType1 = model.AddEntityType("E1");

            entityType1.Annotations.SetClrType(typeof(string));
            model.AddEntitySet("E1Set", entityType1);

            var entityType2 = model.AddEntityType("E2");

            entityType2.Annotations.SetClrType(typeof(string));
            model.AddEntitySet("E2Set", entityType2);

            var entityTypeConfiguration = new EntityTypeConfiguration(typeof(object));

            entityType1.SetConfiguration(entityTypeConfiguration);
            entityType2.SetConfiguration(entityTypeConfiguration);

            model.AddAssociationSet(
                "M2MSet",
                model.AddAssociationType(
                    "M2M",
                    entityType1,
                    RelationshipMultiplicity.Many,
                    entityType2,
                    RelationshipMultiplicity.Many));

            var databaseMapping
                = new DatabaseMappingGenerator(ProviderRegistry.Sql2008_ProviderManifest).Generate(model);

            Assert.Equal(0, databaseMapping.Database.Functions.Count());
        }
Exemplo n.º 3
0
        public void AddAssociationSet_should_create_and_add_to_default_container_explicit_overload()
        {
            var model          = new EdmModel(DataSpace.CSpace);
            var associationSet = new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace));

            model.AddAssociationSet(associationSet);

            Assert.True(model.Containers.Single().AssociationSets.Contains(associationSet));
        }
Exemplo n.º 4
0
        public void AddAssociationSet_should_create_and_add_to_default_container_explicit_overload()
        {
            var model          = new EdmModel().Initialize();
            var associationSet = new AssociationSet("AS", new AssociationType());

            model.AddAssociationSet(associationSet);

            Assert.True(model.Containers.Single().AssociationSets.Contains(associationSet));
        }
        public void Generate_can_map_independent_association_type()
        {
            var model = new EdmModel(DataSpace.CSpace);
            var principalEntityType = model.AddEntityType("P");
            var type = typeof(object);

            principalEntityType.Annotations.SetClrType(type);
            var property = EdmProperty.Primitive("Id1", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            principalEntityType.AddMember(property);
            var idProperty1 = property;

            principalEntityType.AddKeyMember(idProperty1);
            var property1 = EdmProperty.Primitive("Id2", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            principalEntityType.AddMember(property1);
            var idProperty2 = property1;

            principalEntityType.AddKeyMember(idProperty2);
            var dependentEntityType = model.AddEntityType("D");
            var type1 = typeof(string);

            dependentEntityType.Annotations.SetClrType(type1);
            model.AddEntitySet("PSet", principalEntityType);
            model.AddEntitySet("DSet", dependentEntityType);
            var associationType
                = model.AddAssociationType(
                      "P_D",
                      principalEntityType, RelationshipMultiplicity.One,
                      dependentEntityType, RelationshipMultiplicity.Many);

            model.AddAssociationSet("P_DSet", associationType);
            associationType.SourceEnd.DeleteBehavior = OperationAction.Cascade;

            var databaseMapping = new DatabaseMappingGenerator(ProviderRegistry.Sql2008_ProviderManifest).Generate(model);

            var foreignKeyConstraint
                =
                    databaseMapping.GetEntityTypeMapping(dependentEntityType).MappingFragments.Single().Table.ForeignKeyBuilders.Single();

            Assert.Equal(2, foreignKeyConstraint.DependentColumns.Count());
            Assert.Equal(associationType.Name, foreignKeyConstraint.Name);
            Assert.Equal(1, databaseMapping.EntityContainerMappings.Single().AssociationSetMappings.Count());
            Assert.Equal(OperationAction.Cascade, foreignKeyConstraint.DeleteAction);

            var foreignKeyColumn = foreignKeyConstraint.DependentColumns.First();

            Assert.False(foreignKeyColumn.Nullable);
            Assert.Equal("P_Id1", foreignKeyColumn.Name);
        }
Exemplo n.º 6
0
        public void GetAssociationSet_should_return_association_set()
        {
            var model      = new EdmModel().Initialize();
            var entityType = model.AddEntityType("Foo");

            model.AddEntitySet("FooESet", entityType);
            var associationType = new AssociationType();

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

            model.AddAssociationSet("FooSet", associationType);

            var associationSet = model.GetAssociationSet(associationType);

            Assert.NotNull(associationSet);
            Assert.Same(associationType, associationSet.ElementType);
        }
Exemplo n.º 7
0
        public void GetAssociationSet_should_return_association_set()
        {
            var model      = new EdmModel(DataSpace.CSpace);
            var entityType = model.AddEntityType("Foo");

            model.AddEntitySet("FooESet", entityType);
            var associationType
                = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace)
                {
                SourceEnd = new AssociationEndMember("S", entityType),
                TargetEnd = new AssociationEndMember("T", entityType)
                };

            model.AddAssociationSet("FooSet", associationType);

            var associationSet = model.GetAssociationSet(associationType);

            Assert.NotNull(associationSet);
            Assert.Same(associationType, associationSet.ElementType);
        }
        private EdmModel CreateModelWithAssociations(params EdmProperty[] foreignKeys)
        {
            var model = new EdmModel(DataSpace.SSpace);

            for (var i = 0; i < foreignKeys.Length; i++)
            {
                var sourceEntityType = new EntityType("E" + i, "N", DataSpace.SSpace);
                var targetEntityType = new EntityType("E" + i, "N", DataSpace.SSpace);

                model.AddEntitySet("S" + i, sourceEntityType);
                model.AddEntitySet("T" + i, targetEntityType);

                var fk = foreignKeys[i];
                var associationType = new AssociationType((fk == null ? "IA" : fk.Name) + i, "MN", false, DataSpace.SSpace)
                {
                    SourceEnd = new AssociationEndMember(
                        "A_Source" + i, sourceEntityType.GetReferenceType(), RelationshipMultiplicity.ZeroOrOne),
                    TargetEnd = new AssociationEndMember(
                        "A_Target" + i, targetEntityType.GetReferenceType(), RelationshipMultiplicity.Many)
                };

                model.AddAssociationType(associationType);

                if (fk != null)
                {
                    var constraint = new ReferentialConstraint(
                        associationType.SourceEnd,
                        associationType.TargetEnd,
                        new[] { new EdmProperty("SourceProperty") },
                        new[] { fk });

                    associationType.Constraint = constraint;
                }

                model.AddAssociationSet("Set" + (fk == null ? "IA" : fk.Name) + i, associationType);
            }

            return(model);
        }
Exemplo n.º 9
0
        public void AddAssociationSet_should_create_and_add_to_default_container()
        {
            var model = new EdmModel(DataSpace.CSpace);

            var sourceEntityType = model.AddEntityType("Source");
            var targetEntityType = model.AddEntityType("Target");

            model.AddEntitySet("S", sourceEntityType);
            model.AddEntitySet("T", targetEntityType);

            var associationType = model.AddAssociationType(
                "Foo",
                sourceEntityType, RelationshipMultiplicity.One,
                targetEntityType, RelationshipMultiplicity.Many);

            var associationSet = model.AddAssociationSet("FooSet", associationType);

            Assert.NotNull(associationSet);
            Assert.Equal("FooSet", associationSet.Name);
            Assert.Same(associationType, associationSet.ElementType);

            Assert.True(model.Containers.Single().AssociationSets.Contains(associationSet));
        }
Exemplo n.º 10
0
        public void RemoveAssociationType_should_remove_type_and_set()
        {
            var model = new EdmModel(DataSpace.CSpace);

            var sourceEntityType = new EntityType("E", "N", DataSpace.CSpace);
            var targetEntityType = new EntityType("E", "N", DataSpace.CSpace);

            model.AddEntitySet("S", sourceEntityType);
            model.AddEntitySet("T", targetEntityType);

            var associationType
                = model.AddAssociationType(
                      "A",
                      sourceEntityType, RelationshipMultiplicity.ZeroOrOne,
                      targetEntityType, RelationshipMultiplicity.Many);

            model.AddAssociationSet("FooSet", associationType);

            model.RemoveAssociationType(associationType);

            Assert.Equal(0, model.AssociationTypes.Count());
            Assert.Equal(0, model.Containers.First().AssociationSets.Count());
        }