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());
        }
Ejemplo n.º 2
0
        public void GetEntitySets_should_return_all_sets()
        {
            var model = new EdmModel(DataSpace.CSpace);

            model.AddEntitySet("S", new EntityType("E", "N", DataSpace.CSpace));
            model.AddEntitySet("T", new EntityType("E", "N", DataSpace.CSpace));

            Assert.Equal(2, model.GetEntitySets().Count());
        }
Ejemplo n.º 3
0
        public void GetEntitySets_should_return_all_sets()
        {
            var model = new EdmModel().Initialize();

            model.AddEntitySet("S", new EntityType());
            model.AddEntitySet("T", new EntityType());

            Assert.Equal(2, model.GetEntitySets().Count());
        }
        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);
        }
        public void Apply_should_uniquify_names()
        {
            var model = new EdmModel(DataSpace.CSpace);

            model.AddEntitySet("Cats", new EntityType("E", "N", DataSpace.CSpace));
            var entitySet = model.AddEntitySet("Cat", new EntityType("E", "N", DataSpace.CSpace));

            ((IEdmConvention <EntitySet>) new PluralizingEntitySetNameConvention())
            .Apply(entitySet, model);

            Assert.Equal("Cats1", entitySet.Name);
        }
        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);
        }
        public void Map_should_create_association_sets_for_associations()
        {
            var modelConfiguration = new ModelConfiguration();
            var model      = new EdmModel().Initialize();
            var entityType = new EntityType
            {
                Name = "Source"
            };

            model.AddEntitySet("Source", entityType);

            var mappingContext = new MappingContext(modelConfiguration, new ConventionsConfiguration(), model);

            new NavigationPropertyMapper(new TypeMapper(mappingContext))
            .Map(
                new MockPropertyInfo(new MockType("Target"), "Nav"), entityType,
                () => new EntityTypeConfiguration(typeof(object)));

            Assert.Equal(1, model.Containers.Single().AssociationSets.Count);

            var associationSet = model.Containers.Single().AssociationSets.Single();

            Assert.NotNull(associationSet);
            Assert.NotNull(associationSet.ElementType);
            Assert.Equal("Source_Nav", associationSet.Name);
        }
Ejemplo n.º 8
0
        public void Apply_should_uniquify_names()
        {
            var database   = new EdmModel().Initialize();
            var tableA     = new EntityType("T", XmlConstants.TargetNamespace_3, DataSpace.SSpace);
            var tableB     = new EntityType("T", XmlConstants.TargetNamespace_3, DataSpace.SSpace);
            var entitySetA = database.AddEntitySet("ESA", tableA);

            entitySetA.Table = "Customers";
            var entitySetB = database.AddEntitySet("ESB", tableB);

            entitySetB.Table = "Customer";

            ((IDbConvention <EntityType>) new PluralizingTableNameConvention()).Apply(tableB, database);

            Assert.Equal("Customers1", entitySetB.Table);
        }
Ejemplo n.º 9
0
        public void Apply_should_sort_annotated_before_unannotated()
        {
            var table   = new EntityType("T", XmlConstants.TargetNamespace_3, DataSpace.SSpace);
            var columnA = new EdmProperty(
                "C",
                ProviderRegistry.Sql2008_ProviderManifest.GetStoreType(
                    TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String))));

            columnA.SetOrder(2);
            table.AddColumn(columnA);
            table.AddColumn(
                new EdmProperty(
                    "Id",
                    ProviderRegistry.Sql2008_ProviderManifest.GetStoreType(
                        TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)))));

            var database = new EdmModel().Initialize();

            database.AddEntitySet("ES", table);

            ((IDbConvention <EntityType>) new ColumnOrderingConvention()).Apply(table, database);

            Assert.Equal(2, table.Properties.Count);
            Assert.Equal("C", table.Properties.First().Name);
        }
        public void Apply_should_order_by_annotation_if_given()
        {
            var table   = new EntityType("T", XmlConstants.TargetNamespace_3, DataSpace.SSpace);
            var columnA = new EdmProperty(
                "C",
                ProviderRegistry.Sql2008_ProviderManifest.GetStoreType(
                    TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String))));

            columnA.SetOrder(2);
            table.AddColumn(columnA);
            var columnB = new EdmProperty(
                "Id",
                ProviderRegistry.Sql2008_ProviderManifest.GetStoreType(
                    TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String))));

            columnB.SetOrder(1);
            table.AddColumn(columnB);

            var database = new EdmModel(DataSpace.CSpace);

            database.AddEntitySet("ES", table);

            (new ColumnOrderingConvention()).Apply(table, new DbModel(null, database));

            Assert.Equal(2, table.Properties.Count);
            Assert.Equal("Id", table.Properties.First().Name);
        }
        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());
        }
        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_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());
        }
        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 Apply_should_uniquify_names_multiple()
        {
            var model = new EdmModel(DataSpace.CSpace);

            model.AddEntitySet("Cats1", new EntityType("E", "N", DataSpace.CSpace));
            var entitySet1 = model.AddEntitySet("Cats", new EntityType("E", "N", DataSpace.CSpace));
            var entitySet2 = model.AddEntitySet("Cat", new EntityType("E", "N", DataSpace.CSpace));

            (new PluralizingEntitySetNameConvention())
            .Apply(entitySet1, new DbModel(model, null));

            (new PluralizingEntitySetNameConvention())
            .Apply(entitySet2, new DbModel(model, null));

            Assert.Equal("Cats", entitySet1.Name);
            Assert.Equal("Cats2", entitySet2.Name);
        }
Ejemplo n.º 16
0
        public void Validate_should_throw()
        {
            var model = new EdmModel().Initialize();

            model.AddEntitySet("S", new EntityType());

            Assert.Throws <ModelValidationException>(() => model.ValidateCsdl());
        }
Ejemplo n.º 17
0
        public void Validate_should_throw()
        {
            var model = new EdmModel(DataSpace.CSpace);

            model.AddEntitySet("S", new EntityType("E", "N", DataSpace.CSpace));

            Assert.Throws <ModelValidationException>(() => model.Validate());
        }
Ejemplo n.º 18
0
        public void Apply_should_ignore_current_entity_set()
        {
            var model     = new EdmModel().Initialize();
            var entitySet = model.AddEntitySet("Cats", new EntityType());

            ((IEdmConvention <EntitySet>) new PluralizingEntitySetNameConvention())
            .Apply(entitySet, model);

            Assert.Equal("Cats", entitySet.Name);
        }
        public void Apply_should_ignore_current_entity_set()
        {
            var model     = new EdmModel(DataSpace.CSpace);
            var entitySet = model.AddEntitySet("Cats", new EntityType("E", "N", DataSpace.CSpace));

            (new PluralizingEntitySetNameConvention())
            .Apply(entitySet, new DbModel(model, null));

            Assert.Equal("Cats", entitySet.Name);
        }
Ejemplo n.º 20
0
        public void GetValidationErrors_should_return_validation_errors()
        {
            var model = new EdmModel().Initialize();

            model.AddEntitySet("S", new EntityType());

            var validationErrors = model.GetCsdlErrors();

            Assert.Equal(1, validationErrors.Count());
        }
Ejemplo n.º 21
0
        public static System.Data.Entity.Core.Metadata.Edm.EntityType AddTable(
            this EdmModel database,
            string name)
        {
            string str = ((IEnumerable <INamedDataModelItem>)database.EntityTypes).UniquifyName(name);

            System.Data.Entity.Core.Metadata.Edm.EntityType elementType = new System.Data.Entity.Core.Metadata.Edm.EntityType(str, "CodeFirstDatabaseSchema", DataSpace.SSpace);
            database.AddItem(elementType);
            database.AddEntitySet(elementType.Name, elementType, str);
            return(elementType);
        }
Ejemplo n.º 22
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);
        }
Ejemplo n.º 23
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());
        }
        public void Apply_should_set_pluralized_table_name_as_identitier()
        {
            var database  = new EdmModel(DataSpace.CSpace);
            var table     = new EntityType("T", XmlConstants.TargetNamespace_3, DataSpace.SSpace);
            var entitySet = database.AddEntitySet("ES", table);

            entitySet.Table = "Customer";

            ((IDbConvention <EntityType>) new PluralizingTableNameConvention()).Apply(table, database);

            Assert.Equal("Customers", entitySet.Table);
        }
Ejemplo n.º 25
0
        public void Apply_should_ignore_current_table()
        {
            var database  = new EdmModel().Initialize();
            var table     = new EntityType("T", XmlConstants.TargetNamespace_3, DataSpace.SSpace);
            var entitySet = database.AddEntitySet("ES", table);

            entitySet.Table = "Customers";

            ((IDbConvention <EntityType>) new PluralizingTableNameConvention()).Apply(table, database);

            Assert.Equal("Customers", entitySet.Table);
        }
Ejemplo n.º 26
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());
        }
Ejemplo n.º 27
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));
        }
Ejemplo n.º 28
0
        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);
        }
Ejemplo n.º 29
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));
        }
        public void Map_should_map_entity_navigation_properties()
        {
            var model      = new EdmModel(DataSpace.CSpace);
            var entityType = new EntityType("E", "N", DataSpace.CSpace);

            model.AddEntitySet("Source", entityType);
            var mappingContext
                = new MappingContext(new ModelConfiguration(), new ConventionsConfiguration(), model);

            new PropertyMapper(new TypeMapper(mappingContext))
            .Map(new MockPropertyInfo(new MockType(), "Foo"), entityType, () => new EntityTypeConfiguration(typeof(object)));

            Assert.Equal(1, entityType.DeclaredNavigationProperties.Count);
        }