예제 #1
0
        public void HasCascadeDeletePath_should_return_true_for_transitive_cascade()
        {
            var model       = new EdmModel(DataSpace.CSpace);
            var entityTypeA = model.AddEntityType("A");
            var entityTypeB = model.AddEntityType("B");
            var entityTypeC = model.AddEntityType("B");
            var associationTypeA
                = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace)
                {
                SourceEnd = new AssociationEndMember("S", entityTypeA),
                TargetEnd = new AssociationEndMember("T", entityTypeB)
                };

            associationTypeA.SourceEnd.DeleteBehavior = OperationAction.Cascade;
            model.AddAssociationType(associationTypeA);
            var associationTypeB
                = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace)
                {
                SourceEnd = new AssociationEndMember("S", entityTypeB),
                TargetEnd = new AssociationEndMember("T", entityTypeC)
                };

            associationTypeB.SourceEnd.DeleteBehavior = OperationAction.Cascade;
            model.AddAssociationType(associationTypeB);

            Assert.True(model.HasCascadeDeletePath(entityTypeA, entityTypeB));
            Assert.True(model.HasCascadeDeletePath(entityTypeB, entityTypeC));
            Assert.True(model.HasCascadeDeletePath(entityTypeA, entityTypeC));
            Assert.False(model.HasCascadeDeletePath(entityTypeB, entityTypeA));
            Assert.False(model.HasCascadeDeletePath(entityTypeC, entityTypeB));
            Assert.False(model.HasCascadeDeletePath(entityTypeC, entityTypeA));
        }
예제 #2
0
        public void HasCascadeDeletePath_should_return_true_for_transitive_cascade()
        {
            var model            = new EdmModel().Initialize();
            var entityTypeA      = model.AddEntityType("A");
            var entityTypeB      = model.AddEntityType("B");
            var entityTypeC      = model.AddEntityType("B");
            var associationTypeA = new AssociationType();

            associationTypeA.SourceEnd = new AssociationEndMember("S", entityTypeA);
            associationTypeA.TargetEnd = new AssociationEndMember("T", entityTypeB);

            associationTypeA.SourceEnd.DeleteBehavior = OperationAction.Cascade;
            model.AddAssociationType(associationTypeA);
            var associationTypeB = new AssociationType();

            associationTypeB.SourceEnd = new AssociationEndMember("S", entityTypeB);
            associationTypeB.TargetEnd = new AssociationEndMember("T", entityTypeC);

            associationTypeB.SourceEnd.DeleteBehavior = OperationAction.Cascade;
            model.AddAssociationType(associationTypeB);

            Assert.True(model.HasCascadeDeletePath(entityTypeA, entityTypeB));
            Assert.True(model.HasCascadeDeletePath(entityTypeB, entityTypeC));
            Assert.True(model.HasCascadeDeletePath(entityTypeA, entityTypeC));
            Assert.False(model.HasCascadeDeletePath(entityTypeB, entityTypeA));
            Assert.False(model.HasCascadeDeletePath(entityTypeC, entityTypeB));
            Assert.False(model.HasCascadeDeletePath(entityTypeC, entityTypeA));
        }
        public void TypeHierarchyIterator_should_return_entity_types_in_depth_first_order()
        {
            var model          = new EdmModel(DataSpace.CSpace);
            var entityTypeRoot = model.AddEntityType("Root");
            var derivedType1   = model.AddEntityType("DType1");
            var derivedType2   = model.AddEntityType("DType2");

            derivedType1.BaseType = entityTypeRoot;
            derivedType2.BaseType = entityTypeRoot;
            var derivedType1_1 = model.AddEntityType("DType1_1");
            var derivedType1_2 = model.AddEntityType("DType1_2");

            derivedType1_1.BaseType = derivedType1;
            derivedType1_2.BaseType = derivedType1;

            var typesVisited = new List <EntityType>();

            foreach (var derivedType in entityTypeRoot.TypeHierarchyIterator(model))
            {
                typesVisited.Add(derivedType);
            }

            var oracle = new List <EntityType>
            {
                entityTypeRoot,
                derivedType1,
                derivedType1_1,
                derivedType1_2,
                derivedType2
            };

            Assert.Equal(true, oracle.SequenceEqual(typesVisited));
        }
        public void Generate_maps_abstract_type_hierarchies_correctly()
        {
            var model = new EdmModel(DataSpace.CSpace);

            var rootEntityType = model.AddEntityType("E");

            rootEntityType.Annotations.SetClrType(typeof(object));

            var property0
                = EdmProperty.Primitive("P1", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            rootEntityType.AddMember(property0);
            rootEntityType.AddKeyMember(rootEntityType.Properties.First());

            var property1
                = EdmProperty.Primitive("P2", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            rootEntityType.AddMember(property1);

            model.AddEntitySet("ESet", rootEntityType);

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

            var property2
                = EdmProperty.Primitive("P3", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType2.AddMember(property2);
            entityType2.Annotations.SetClrType(typeof(string));
            entityType2.Abstract = true;
            entityType2.BaseType = rootEntityType;

            var entityType3 = model.AddEntityType("E3");

            entityType3.Annotations.SetClrType(typeof(int));

            var property3
                = EdmProperty.Primitive("P4", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType3.AddMember(property3);
            entityType3.BaseType = entityType2;

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

            var entityType1Mapping = databaseMapping.GetEntityTypeMapping(rootEntityType);
            var entityType3Mapping = databaseMapping.GetEntityTypeMapping(entityType3);

            Assert.Equal(2, entityType1Mapping.MappingFragments.Single().ColumnMappings.Count());
            Assert.Equal("P1", entityType1Mapping.MappingFragments.Single().ColumnMappings.ElementAt(0).ColumnProperty.Name);
            Assert.Equal("P2", entityType1Mapping.MappingFragments.Single().ColumnMappings.ElementAt(1).ColumnProperty.Name);

            Assert.Equal(4, entityType3Mapping.MappingFragments.Single().ColumnMappings.Count());
            Assert.Equal("P1", entityType3Mapping.MappingFragments.Single().ColumnMappings.ElementAt(0).ColumnProperty.Name);
            Assert.Equal("P2", entityType3Mapping.MappingFragments.Single().ColumnMappings.ElementAt(1).ColumnProperty.Name);
            Assert.Equal("P3", entityType3Mapping.MappingFragments.Single().ColumnMappings.ElementAt(2).ColumnProperty.Name);
            Assert.Equal("P4", entityType3Mapping.MappingFragments.Single().ColumnMappings.ElementAt(3).ColumnProperty.Name);

            var table = entityType1Mapping.MappingFragments.Single().Table;

            Assert.Equal(5, table.Properties.Count);
        }
        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());
        }
        public void Generate_can_map_type_hierarchies_using_Tph()
        {
            var model          = new EdmModel(DataSpace.CSpace);
            var rootEntityType = model.AddEntityType("E");
            var type           = typeof(object);

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

            rootEntityType.AddMember(property);
            var property1 = EdmProperty.Primitive("P2", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            rootEntityType.AddMember(property1);
            var entitySet   = model.AddEntitySet("ESet", rootEntityType);
            var entityType2 = model.AddEntityType("E2");
            var property2   = EdmProperty.Primitive("P3", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType2.AddMember(property2);
            var type1 = typeof(string);

            entityType2.Annotations.SetClrType(type1);
            entityType2.BaseType = rootEntityType;
            var entityType3 = model.AddEntityType("E3");
            var type2       = typeof(int);

            entityType3.Annotations.SetClrType(type2);
            var property3 = EdmProperty.Primitive("P4", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType3.AddMember(property3);
            entityType3.BaseType = entityType2;

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

            var entitySetMapping = databaseMapping.GetEntitySetMapping(entitySet);

            Assert.NotNull(entitySetMapping);
            var entityTypeMappings = entitySetMapping.EntityTypeMappings;

            Assert.Equal(3, entityTypeMappings.Count());

            var entityType1Mapping = databaseMapping.GetEntityTypeMapping(rootEntityType);
            var entityType2Mapping = databaseMapping.GetEntityTypeMapping(entityType2);
            var entityType3Mapping = databaseMapping.GetEntityTypeMapping(entityType3);

            Assert.Equal(2, entityType1Mapping.MappingFragments.Single().ColumnMappings.Count());
            Assert.Equal(3, entityType2Mapping.MappingFragments.Single().ColumnMappings.Count());
            Assert.Equal(4, entityType3Mapping.MappingFragments.Single().ColumnMappings.Count());

            var table = entityType1Mapping.MappingFragments.Single().Table;

            Assert.Same(table, entityType2Mapping.MappingFragments.Single().Table);
            Assert.Same(table, entityType3Mapping.MappingFragments.Single().Table);
            Assert.Equal(5, table.Properties.Count);
            Assert.Equal("P1", table.Properties[0].Name);
            Assert.Equal("P2", table.Properties[1].Name);
            Assert.Equal("P3", table.Properties[2].Name);
            Assert.Equal("P4", table.Properties[3].Name);
            Assert.Equal("Discriminator", table.Properties[4].Name);
        }
        public void Generate_can_map_foreign_key_association_type()
        {
            var model = new EdmModel(DataSpace.CSpace);

            var principalEntityType = model.AddEntityType("P");

            principalEntityType.Annotations.SetClrType(typeof(object));

            var dependentEntityType = model.AddEntityType("D");

            dependentEntityType.Annotations.SetClrType(typeof(string));

            var dependentProperty1 = EdmProperty.Primitive("FK1", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32));

            dependentProperty1.Nullable = false;
            dependentEntityType.AddMember(dependentProperty1);

            var dependentProperty2 = EdmProperty.Primitive("FK2", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            dependentEntityType.AddMember(dependentProperty2);

            model.AddEntitySet("PSet", principalEntityType);
            model.AddEntitySet("DSet", dependentEntityType);

            var associationType
                = model.AddAssociationType(
                      "P_D",
                      principalEntityType, RelationshipMultiplicity.One,
                      dependentEntityType, RelationshipMultiplicity.Many);

            associationType.Constraint
                = new ReferentialConstraint(
                      associationType.SourceEnd,
                      associationType.TargetEnd,
                      principalEntityType.KeyProperties,
                      new[] { dependentProperty1, dependentProperty2 });

            associationType.SourceEnd.DeleteBehavior = OperationAction.Cascade;

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

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

            Assert.Equal(2, dependentTable.Properties.Count());
            Assert.Equal(2, foreignKeyConstraint.DependentColumns.Count());
            Assert.Equal(OperationAction.Cascade, foreignKeyConstraint.DeleteAction);
            Assert.Equal(associationType.Name, foreignKeyConstraint.Name);

            var foreignKeyColumn = foreignKeyConstraint.DependentColumns.First();

            Assert.False(foreignKeyColumn.Nullable);
            Assert.Equal("FK1", foreignKeyColumn.Name);
        }
예제 #8
0
        public void EdmModelAddEntityTypeTest()
        {
            var model       = new EdmModel();
            var entityTypeA = model.AddEntityType("NS", "EntityA");
            var entityTypeB = model.AddEntityType("NS", "EntityB", entityTypeA);
            var entityTypeC = model.AddEntityType("NS", "EntityC", entityTypeB, false, true);
            var entityTypeD = model.AddEntityType("NS", "EntityD", entityTypeC, false, true, true);

            Assert.Equal(entityTypeA, model.FindDeclaredType("NS.EntityA"));
            Assert.Equal(entityTypeB, model.FindDeclaredType("NS.EntityB"));
            Assert.Equal(entityTypeC, model.FindDeclaredType("NS.EntityC"));
            Assert.Equal(entityTypeD, model.FindDeclaredType("NS.EntityD"));
        }
예제 #9
0
        public void GetClrTypes_should_return_ospace_types()
        {
            var model          = new EdmModel(DataSpace.CSpace);
            var type1          = typeof(object);
            var tempQualifier1 = model.AddEntityType("A");

            tempQualifier1.Annotations.SetClrType(type1);
            var type = typeof(string);

            model.AddEntityType("B").Annotations.SetClrType(type);

            Assert.Equal(2, model.GetClrTypes().Count());
        }
        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);
        }
예제 #11
0
        public void Configure_should_configure_inverse()
        {
            var inverseMockPropertyInfo         = new MockPropertyInfo();
            var navigationPropertyConfiguration = new NavigationPropertyConfiguration(new MockPropertyInfo())
            {
                InverseNavigationProperty = inverseMockPropertyInfo
            };
            var associationType = new AssociationType();

            associationType.SourceEnd = new AssociationEndMember("S", new EntityType());
            associationType.TargetEnd = new AssociationEndMember("T", new EntityType());
            var inverseAssociationType = new AssociationType();

            inverseAssociationType.SourceEnd = new AssociationEndMember("S", new EntityType());
            inverseAssociationType.TargetEnd = new AssociationEndMember("T", new EntityType());
            var model = new EdmModel().Initialize();

            model.AddAssociationType(inverseAssociationType);
            var inverseNavigationProperty
                = model.AddEntityType("T")
                  .AddNavigationProperty("N", inverseAssociationType);

            inverseNavigationProperty.SetClrPropertyInfo(inverseMockPropertyInfo);

            navigationPropertyConfiguration.Configure(
                new NavigationProperty("N", TypeUsage.Create(associationType.TargetEnd.GetEntityType()))
            {
                RelationshipType = associationType
            }, model, new EntityTypeConfiguration(typeof(object)));

            Assert.Same(associationType, inverseNavigationProperty.Association);
            Assert.Same(associationType.SourceEnd, inverseNavigationProperty.ResultEnd);
            Assert.Equal(0, model.GetAssociationTypes().Count());
        }
        public void Apply_generates_constraint_when_composite_fk()
        {
            var model           = new EdmModel(DataSpace.CSpace);
            var entityType      = model.AddEntityType("E");
            var associationType = model.AddAssociationType(
                "A",
                entityType, RelationshipMultiplicity.ZeroOrOne,
                entityType, RelationshipMultiplicity.Many);
            var navigationProperty = entityType.AddNavigationProperty("N", associationType);
            var property           = EdmProperty.Primitive("Fk1", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property);
            var fkProperty1 = property;
            var property1   = EdmProperty.Primitive("Fk2", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property1);
            var fkProperty2          = property1;
            var foreignKeyAnnotation = new ForeignKeyAttribute("Fk2,Fk1");

            navigationProperty.Annotations.SetClrAttributes(new[] { foreignKeyAnnotation });

            ((IEdmConvention <NavigationProperty>) new ForeignKeyNavigationPropertyAttributeConvention())
            .Apply(navigationProperty, model);

            Assert.NotNull(associationType.Constraint);
            Assert.True(new[] { fkProperty2, fkProperty1 }.SequenceEqual(associationType.Constraint.ToProperties));
        }
        public void Configure_should_configure_inverse()
        {
            var inverseMockPropertyInfo         = new MockPropertyInfo();
            var navigationPropertyConfiguration = new NavigationPropertyConfiguration(new MockPropertyInfo(typeof(AType1), "N"))
            {
                InverseNavigationProperty = inverseMockPropertyInfo
            };
            var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace);

            associationType.SourceEnd = new AssociationEndMember("S", new EntityType("E", "N", DataSpace.CSpace));
            associationType.TargetEnd = new AssociationEndMember("T", new EntityType("E", "N", DataSpace.CSpace));
            var inverseAssociationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace);

            inverseAssociationType.SourceEnd = new AssociationEndMember("S", new EntityType("E", "N", DataSpace.CSpace));
            inverseAssociationType.TargetEnd = new AssociationEndMember("T", new EntityType("E", "N", DataSpace.CSpace));
            var model = new EdmModel(DataSpace.CSpace);

            model.AddAssociationType(inverseAssociationType);
            var inverseNavigationProperty
                = model.AddEntityType("T")
                  .AddNavigationProperty("N", inverseAssociationType);

            inverseNavigationProperty.SetClrPropertyInfo(inverseMockPropertyInfo);

            navigationPropertyConfiguration.Configure(
                new NavigationProperty("N", TypeUsage.Create(associationType.TargetEnd.GetEntityType()))
            {
                RelationshipType = associationType
            }, model, new EntityTypeConfiguration(typeof(object)));

            Assert.Same(associationType, inverseNavigationProperty.Association);
            Assert.Same(associationType.SourceEnd, inverseNavigationProperty.ResultEnd);
            Assert.Same(associationType.TargetEnd, inverseNavigationProperty.FromEndMember);
            Assert.Equal(0, model.AssociationTypes.Count());
        }
        public void Configure_should_configure_modification_functions()
        {
            var model = new EdmModel(DataSpace.CSpace);

            var entityType = model.AddEntityType("E");

            entityType.GetMetadataProperties().SetClrType(typeof(object));

            model.AddEntitySet("ESet", entityType);

            var modificationFunctionsConfigurationMock = new Mock <ModificationStoredProceduresConfiguration>();

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

            entityTypeConfiguration.MapToStoredProcedures(modificationFunctionsConfigurationMock.Object, true);

            entityType.SetConfiguration(entityTypeConfiguration);

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

            entityTypeConfiguration.Configure(entityType, databaseMapping, ProviderRegistry.Sql2008_ProviderManifest);

            modificationFunctionsConfigurationMock
            .Verify(
                m => m.Configure(
                    It.IsAny <EntityTypeModificationFunctionMapping>(), It.IsAny <DbProviderManifest>()),
                Times.Once());
        }
예제 #15
0
        public void GetStructuralOrEnumType_should_return_correct_type_for_EntityType()
        {
            var model      = new EdmModel(DataSpace.CSpace);
            var entityType = model.AddEntityType("Foo");

            Assert.Same(entityType, model.GetStructuralOrEnumType("Foo"));
        }
        public void Generate_can_map_a_simple_entity_type_and_set()
        {
            var model      = new EdmModel(DataSpace.CSpace);
            var entityType = model.AddEntityType("E");
            var type       = typeof(object);

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

            entityType.AddMember(property);
            var property1 = EdmProperty.Primitive("P2", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property1);
            var entitySet = model.AddEntitySet("ESet", entityType);

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

            var entitySetMapping = databaseMapping.GetEntitySetMapping(entitySet);

            Assert.NotNull(entitySetMapping);
            Assert.Same(entitySet, entitySetMapping.EntitySet);

            var entityTypeMapping = entitySetMapping.EntityTypeMappings.Single();

            Assert.Same(entityType, entityTypeMapping.EntityType);
            Assert.NotNull(entityTypeMapping.MappingFragments.Single().Table);
            Assert.Equal("E", entityTypeMapping.MappingFragments.Single().Table.Name);
            Assert.Equal(2, entityTypeMapping.MappingFragments.Single().Table.Properties.Count);
            Assert.Equal(typeof(object), entityTypeMapping.GetClrType());
        }
예제 #17
0
        private EdmStructuredType AddStructuredType(RdmStructuredType definition)
        {
            // base type, the Build method ensure that the base type was added before the
            // sub-type and therefore FindType will succeed.
            IEdmStructuredType edmBaseType = null;

            if (definition.BaseType != null)
            {
                edmBaseType = edmModel.FindType(rdmModel.Namespace.NamespaceName + "." + definition.BaseType) as EdmStructuredType;
                if (edmBaseType == null)
                {
                    throw new TransformationException($"unable to find base type {definition.BaseType} for type {definition.Name}");
                }
            }

            if (definition.Keys.Any() || HasSingletonOfType(definition) || (edmBaseType != null && edmBaseType.TypeKind == EdmTypeKind.Entity))
            {
                var entity = edmModel.AddEntityType(rdmModel.Namespace.NamespaceName, definition.Name, (IEdmEntityType)edmBaseType, definition.IsAbstract, true);
                foreach (var annotation in definition.Annotations)
                {
                    annotationBuilder.AddAnnotation(edmModel, entity, annotation);
                }
                return(entity);
            }
            else
            {
                var complex = edmModel.AddComplexType(rdmModel.Namespace.NamespaceName, definition.Name, (IEdmComplexType)edmBaseType, definition.IsAbstract, true);
                foreach (var annotation in definition.Annotations)
                {
                    annotationBuilder.AddAnnotation(edmModel, complex, annotation);
                }
                return(complex);
            }
        }
예제 #18
0
        public void GetStructuralType_should_return_entity_or_complex_type()
        {
            var model       = new EdmModel(DataSpace.CSpace);
            var entityType  = model.AddEntityType("E");
            var complexType = model.AddComplexType("C");

            Assert.Same(entityType, model.GetStructuralType("E"));
            Assert.Same(complexType, model.GetStructuralType("C"));
        }
예제 #19
0
        public void GetDerivedTypes_must_return_list_of_direct_descendants()
        {
            var model   = new EdmModel(DataSpace.CSpace);
            var entity1 = model.AddEntityType("E1");
            var entity2 = model.AddEntityType("E2");
            var entity3 = model.AddEntityType("E3");
            var entity4 = model.AddEntityType("E4");

            entity2.BaseType = entity1;
            entity3.BaseType = entity1;
            entity4.BaseType = entity2;

            var derivedTypes = model.GetDerivedTypes(entity1).ToList();

            Assert.Equal(2, derivedTypes.Count);
            Assert.Same(entity2, derivedTypes[0]);
            Assert.Same(entity3, derivedTypes[1]);
        }
예제 #20
0
        public void GetEntityType_should_return_correct_type()
        {
            var model      = new EdmModel(DataSpace.CSpace);
            var entityType = model.AddEntityType("Foo");

            var foundEntityType = model.GetEntityType("Foo");

            Assert.NotNull(foundEntityType);
            Assert.Same(entityType, foundEntityType);
        }
예제 #21
0
        public void AddEntityType_should_create_and_add_to_default_namespace()
        {
            var model = new EdmModel().Initialize();

            var entityType = model.AddEntityType("Foo");

            Assert.NotNull(entityType);
            Assert.Equal("Foo", entityType.Name);
            Assert.True(model.Namespaces.Single().EntityTypes.Contains(entityType));
        }
예제 #22
0
        public void AddEntityType_should_create_and_add_with_default_namespace()
        {
            var model = new EdmModel(DataSpace.CSpace);

            var entityType = model.AddEntityType("Foo");

            Assert.NotNull(entityType);
            Assert.Equal("Foo", entityType.Name);
            Assert.Equal(EdmModelExtensions.DefaultModelNamespace, entityType.NamespaceName);
            Assert.True(model.EntityTypes.Contains(entityType));
        }
예제 #23
0
        public void AddEntityType_should_create_and_add_when_custom_namespace()
        {
            var model = new EdmModel(DataSpace.CSpace);

            var entityType = model.AddEntityType("Foo", "Bar");

            Assert.NotNull(entityType);
            Assert.Equal("Foo", entityType.Name);
            Assert.Equal("Bar", entityType.NamespaceName);
            Assert.True(model.EntityTypes.Contains(entityType));
        }
예제 #24
0
        public void AddAssociationType_should_create_and_add_to_default_namespace()
        {
            var model = new EdmModel().Initialize();

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

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

            Assert.NotNull(associationType);
            Assert.Equal("Foo", associationType.Name);
            Assert.Same(sourceEntityType, associationType.SourceEnd.GetEntityType());
            Assert.Equal(RelationshipMultiplicity.One, associationType.SourceEnd.RelationshipMultiplicity);
            Assert.Same(targetEntityType, associationType.TargetEnd.GetEntityType());
            Assert.Equal(RelationshipMultiplicity.Many, associationType.TargetEnd.RelationshipMultiplicity);
            Assert.True(model.Namespaces.Single().AssociationTypes.Contains(associationType));
        }
예제 #25
0
        public void AddEntitySet_should_create_and_add_to_default_container()
        {
            var model      = new EdmModel(DataSpace.CSpace);
            var entityType = model.AddEntityType("Foo");

            var entitySet = model.AddEntitySet("FooSet", entityType);

            Assert.NotNull(entitySet);
            Assert.Equal("FooSet", entitySet.Name);
            Assert.Same(entityType, entitySet.ElementType);
            Assert.True(model.Containers.Single().EntitySets.Contains(entitySet));
        }
예제 #26
0
        public void AddAssociationType_should_create_and_add_with_default_namespace()
        {
            var model = new EdmModel(DataSpace.CSpace);

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

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

            Assert.NotNull(associationType);
            Assert.Equal("Foo", associationType.Name);
            Assert.Equal(EdmModelExtensions.DefaultModelNamespace, associationType.NamespaceName);
            Assert.Same(sourceEntityType, associationType.SourceEnd.GetEntityType());
            Assert.Equal(RelationshipMultiplicity.One, associationType.SourceEnd.RelationshipMultiplicity);
            Assert.Same(targetEntityType, associationType.TargetEnd.GetEntityType());
            Assert.Equal(RelationshipMultiplicity.Many, associationType.TargetEnd.RelationshipMultiplicity);
            Assert.True(model.AssociationTypes.Contains(associationType));
        }
예제 #27
0
        private static EdmModel CreateModelFixture(
            out EntityType declaringEntityType, out EntityType complexEntityType)
        {
            var model = new EdmModel().Initialize();

            declaringEntityType = model.AddEntityType("E");
            complexEntityType   = model.AddEntityType("C");
            var property = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            complexEntityType.AddMember(property);

            var associationType
                = model.AddAssociationType(
                      "A",
                      declaringEntityType, RelationshipMultiplicity.Many,
                      complexEntityType, RelationshipMultiplicity.ZeroOrOne);

            declaringEntityType.AddNavigationProperty("E.C", associationType);

            return(model);
        }
예제 #28
0
        public void GetEntitySet_should_return_entity_set()
        {
            var model      = new EdmModel(DataSpace.CSpace);
            var entityType = model.AddEntityType("Foo");

            model.AddEntitySet("FooSet", entityType);

            var entitySet = model.GetEntitySet(entityType);

            Assert.NotNull(entitySet);
            Assert.Same(entityType, entitySet.ElementType);
        }
예제 #29
0
        public void RemoveEntityType_should_remove_type_and_set()
        {
            var model      = new EdmModel(DataSpace.CSpace);
            var entityType = model.AddEntityType("Foo");

            model.AddEntitySet("FooSet", entityType);

            model.RemoveEntityType(entityType);

            Assert.Equal(0, model.EntityTypes.Count());
            Assert.Equal(0, model.Containers.First().EntitySets.Count());
        }
예제 #30
0
        public void ReplaceEntitySet_should_remove_set_with_type()
        {
            var model      = new EdmModel().Initialize();
            var entityType = model.AddEntityType("Foo");

            model.AddEntitySet("FooSet", entityType);

            model.ReplaceEntitySet(entityType, null);

            Assert.Equal(1, model.GetEntityTypes().Count());
            Assert.Equal(0, model.Containers.First().EntitySets.Count());
        }