Beispiel #1
0
        private DbModel Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
        {
            DebugCheck.NotNull(providerManifest);
            DebugCheck.NotNull(providerInfo);

            var model = new EdmModel().Initialize(_modelBuilderVersion.GetEdmVersion());

            model.SetProviderInfo(providerInfo);

            _conventionsConfiguration.ApplyModelConfiguration(_modelConfiguration);

            _modelConfiguration.NormalizeConfigurations();

            MapTypes(model);

            _modelConfiguration.Configure(model);
            _conventionsConfiguration.ApplyModel(model);

            model.ValidateCsdl();

            var databaseMapping = model.GenerateDatabaseMapping(providerManifest);

            //Running the PluralizingTableNameConvention first so that the new table name is available for configuration
            _conventionsConfiguration.ApplyPluralizingTableNameConvention(databaseMapping.Database);
            _modelConfiguration.Configure(databaseMapping, providerManifest);
            _conventionsConfiguration.ApplyDatabase(databaseMapping.Database);
            _conventionsConfiguration.ApplyMapping(databaseMapping);

            databaseMapping.Database.SetProviderInfo(providerInfo);

            return(new DbModel(databaseMapping, Clone()));
        }
Beispiel #2
0
        private DbModel Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
        {
            DebugCheck.NotNull(providerManifest);
            DebugCheck.NotNull(providerInfo);

            var schemaVersion     = _modelBuilderVersion.GetEdmVersion();
            var modelBuilderClone = Clone();

            var model = new DbModel(
                new DbDatabaseMapping()
            {
                Model    = EdmModel.CreateConceptualModel(schemaVersion),
                Database = EdmModel.CreateStoreModel(providerInfo, providerManifest, schemaVersion)
            },
                modelBuilderClone);

            model.ConceptualModel.Container.AddAnnotation(XmlConstants.UseClrTypesAnnotationWithPrefix, "true");

            _conventionsConfiguration.ApplyModelConfiguration(_modelConfiguration);

            _modelConfiguration.NormalizeConfigurations();

            MapTypes(model.ConceptualModel);

            _modelConfiguration.Configure(model.ConceptualModel);

            _conventionsConfiguration.ApplyConceptualModel(model);

            model.ConceptualModel.Validate();

            model = new DbModel(
                model.ConceptualModel.GenerateDatabaseMapping(providerInfo, providerManifest),
                modelBuilderClone);

            // Run the PluralizingTableNameConvention first so that the new table name is available for configuration
            _conventionsConfiguration.ApplyPluralizingTableNameConvention(model);

            _modelConfiguration.Configure(model.DatabaseMapping, providerManifest);

            _conventionsConfiguration.ApplyStoreModel(model);

            _conventionsConfiguration.ApplyMapping(model.DatabaseMapping);

            model.StoreModel.Validate();

            return(model);
        }
        public void Configure_should_uniquify_unconfigured_function_names()
        {
            var modelConfiguration = new ModelConfiguration();

            var typeA = new MockType("A");
            var typeB = new MockType("B");

            modelConfiguration.Entity(typeA).MapToStoredProcedures();

            var modificationFunctionsConfiguration
                = new ModificationStoredProceduresConfiguration();

            var modificationFunctionConfiguration = new ModificationStoredProcedureConfiguration();
            modificationFunctionConfiguration.HasName("A_Insert");

            modificationFunctionsConfiguration.Insert(modificationFunctionConfiguration);

            modelConfiguration.Entity(typeB).MapToStoredProcedures(modificationFunctionsConfiguration, true);

            var model = new EdmModel(DataSpace.CSpace);

            var entityA = model.AddEntityType("A");
            entityA.GetMetadataProperties().SetClrType(typeA);
            entityA.SetConfiguration(modelConfiguration.Entity(typeA));

            var entityB = model.AddEntityType("B");
            entityB.GetMetadataProperties().SetClrType(typeB);
            entityB.SetConfiguration(modelConfiguration.Entity(typeB));

            model.AddEntitySet("AS", entityA);
            model.AddEntitySet("BS", entityB);

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

            modelConfiguration.Configure(databaseMapping, ProviderRegistry.Sql2008_ProviderManifest);

            Assert.True(databaseMapping.Database.Functions.Any(f => f.StoreFunctionNameAttribute == "A_Insert"));
            Assert.True(databaseMapping.Database.Functions.Any(f => f.StoreFunctionNameAttribute == "A_Insert1"));
        }
        public void TPC_with_IA_on_non_leaf_type_not_allowed()
        {
            new Exception("The association 'Related_to_Base' between entity types 'Related' and 'Base' is invalid. In a TPC hierarchy independent associations are only allowed on the most derived types.")
                .ValidateMessage("EntityMappingConfiguration_TPCWithIAsOnNonLeafType", "Related_to_Base", "Related", "Base");

            var modelBuilder = 
             new TestModelBuilder()
                .Entity("Base")
                .Key("P1")
                .Property("P2")
                .Subclass("Derived");

            modelBuilder.Entity("Related");
            modelBuilder.Association(
                "Related_To_Base", "Related", RelationshipMultiplicity.One, "BaseEntities", 
                "Base", RelationshipMultiplicity.Many, "RelatedEntities");

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

            var modelConfiguration = new ModelConfiguration();
            modelConfiguration.Entity(model.GetEntityType("Derived").GetClrType())
                              .AddMappingConfiguration(
                                  new EntityMappingConfiguration
                                  {
                                      MapInheritedProperties = true,
                                      TableName = new DatabaseName("DerivedEntities")
                                  });

            Assert.Throws<InvalidOperationException>(
                () => modelConfiguration.Configure(databaseMapping, ProviderRegistry.Sql2008_ProviderManifest))
                .ValidateMessage("EntityMappingConfiguration_TPCWithIAsOnNonLeafType", "Related_To_Base", "Related", "Base");
        }
        public void Configure_mapping_can_process_simple_TPH_mapping()
        {
            //Setup
            var model = TestModelBuilderHelpers.CreateSimpleInheritanceTwoEntities();
            var databaseMapping = new DatabaseMappingGenerator(ProviderRegistry.Sql2008_ProviderInfo, ProviderRegistry.Sql2008_ProviderManifest).Generate(model);

            var entityType1 = model.GetEntityType("E1");
            var entityType2 = model.GetEntityType("E2");

            // Action
            var modelConfiguration = new ModelConfiguration();
            var entity1Configuration = modelConfiguration.Entity(entityType1.GetClrType());
            var entity1MappingConfiguration =
                new EntityMappingConfiguration
                    {
                        TableName = new DatabaseName("E1")
                    };
            entity1MappingConfiguration
                .AddValueCondition(
                    new ValueConditionConfiguration(entity1MappingConfiguration, "disc")
                        {
                            Value = "foo"
                        });
            entity1Configuration.AddMappingConfiguration(entity1MappingConfiguration);
            var entity1SubTypeMappingConfiguration =
                new EntityMappingConfiguration
                    {
                        TableName = new DatabaseName("E1")
                    };
            entity1SubTypeMappingConfiguration
                .AddValueCondition(
                    new ValueConditionConfiguration(entity1SubTypeMappingConfiguration, "disc")
                        {
                            Value = "bar"
                        });
            entity1Configuration.AddSubTypeMappingConfiguration(entityType2.GetClrType(), entity1SubTypeMappingConfiguration);
            modelConfiguration.NormalizeConfigurations();
            modelConfiguration.Configure(databaseMapping, ProviderRegistry.Sql2008_ProviderManifest);

            //Validate
            var entitySetMapping = databaseMapping.GetEntitySetMapping(model.GetEntitySet(entityType1));
            Assert.NotNull(entitySetMapping);
            Assert.Equal(3, entitySetMapping.EntityTypeMappings.Count());
            var entityType1Mapping = databaseMapping.GetEntityTypeMapping(entityType1);
            var entityType1MappingConditions = databaseMapping.GetEntityTypeMappings(entityType1).Single(tm => !tm.IsHierarchyMapping);
            var entityType2Mapping = databaseMapping.GetEntityTypeMapping(entityType2);

            var table1 = entityType1Mapping.MappingFragments.Single().Table;
            var table2 = entityType2Mapping.MappingFragments.Single().Table;

            Assert.True(entityType1Mapping.IsHierarchyMapping);
            Assert.Equal(4, table1.Properties.Count);
            Assert.Equal("P1", table1.Properties[0].Name);
            Assert.Equal("P2", table1.Properties[1].Name);
            Assert.Equal("P3", table1.Properties[2].Name);
            Assert.Equal("disc", table1.Properties[3].Name);
            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.Same(
                table1.Properties[3], entityType1MappingConditions.MappingFragments.Single().ColumnConditions.Single().Column);
            Assert.Equal("foo", entityType1MappingConditions.MappingFragments.Single().ColumnConditions.Single().Value);
            Assert.Equal(
                "nvarchar", entityType1MappingConditions.MappingFragments.Single().ColumnConditions.Single().Column.TypeName);
            Assert.Equal(
                DatabaseMappingGenerator.DiscriminatorMaxLength,
                entityType1MappingConditions.MappingFragments.Single().ColumnConditions.Single().Column.MaxLength);

            Assert.False(entityType2Mapping.IsHierarchyMapping);
            Assert.Same(table1, table2);
            Assert.Equal(2, entityType2Mapping.MappingFragments.Single().ColumnMappings.Count());
            Assert.Same(table1.Properties[0], table2.Properties[0]);
            Assert.Same(table1.Properties[1], table2.Properties[1]);
            Assert.Equal("P1", entityType2Mapping.MappingFragments.Single().ColumnMappings.ElementAt(0).ColumnProperty.Name);
            Assert.Equal("P3", entityType2Mapping.MappingFragments.Single().ColumnMappings.ElementAt(1).ColumnProperty.Name);
            Assert.Same(table2.Properties[3], entityType2Mapping.MappingFragments.Single().ColumnConditions.Single().Column);
            Assert.Equal("bar", entityType2Mapping.MappingFragments.Single().ColumnConditions.Single().Value);
        }
        public void Configure_should_uniquify_unconfigured_association_function_names()
        {
            var mockPropertyInfo = typeof(BType1).GetDeclaredProperty("As");

            var modelConfiguration = new ModelConfiguration();

            var navigationPropertyConfiguration
                = modelConfiguration.Entity(typeof(BType1)).Navigation(mockPropertyInfo);

            navigationPropertyConfiguration.ModificationStoredProceduresConfiguration
                = new ModificationStoredProceduresConfiguration();

            var modificationFunctionConfiguration = new ModificationStoredProcedureConfiguration();
            modificationFunctionConfiguration.HasName("AB_Delete");

            navigationPropertyConfiguration.ModificationStoredProceduresConfiguration
                .Insert(modificationFunctionConfiguration);

            var model = new EdmModel(DataSpace.CSpace);

            var entityA = model.AddEntityType("A");
            entityA.GetMetadataProperties().SetClrType(typeof(AType1));
            entityA.SetConfiguration(modelConfiguration.Entity(typeof(AType1)));

            var entityB = model.AddEntityType("B");
            entityB.GetMetadataProperties().SetClrType(typeof(BType1));
            entityB.SetConfiguration(modelConfiguration.Entity(typeof(BType1)));

            model.AddEntitySet("AS", entityA);
            model.AddEntitySet("BS", entityB);

            var associationType
                = model.AddAssociationType(
                    "M2M",
                    entityA,
                    RelationshipMultiplicity.Many,
                    entityB,
                    RelationshipMultiplicity.Many);

            associationType.SetConfiguration(navigationPropertyConfiguration);

            var navigationProperty
                = entityB.AddNavigationProperty("As", associationType);

            navigationProperty.SetClrPropertyInfo(mockPropertyInfo);

            model.AddAssociationSet("M2MSet", associationType);

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

            modelConfiguration.Configure(databaseMapping, ProviderRegistry.Sql2008_ProviderManifest);

            Assert.True(databaseMapping.Database.Functions.Any(f => f.StoreFunctionNameAttribute == "AB_Delete"));
            Assert.True(databaseMapping.Database.Functions.Any(f => f.StoreFunctionNameAttribute == "AB_Delete1"));
        }
        public void Configure_mapping_can_process_one_level_TPC_on_both_sides_of_tree()
        {
            //Setup
            var model = TestModelBuilderHelpers.CreateSingleLevelInheritanceWithThreeEntities();
            var databaseMapping = new DatabaseMappingGenerator(ProviderRegistry.Sql2008_ProviderInfo, ProviderRegistry.Sql2008_ProviderManifest).Generate(model);

            var entityType1 = model.GetEntityType("E1");
            var entityType2 = model.GetEntityType("E2");
            var entityType3 = model.GetEntityType("E3");

            // Action
            var modelConfiguration = new ModelConfiguration();
            modelConfiguration.Entity(entityType2.GetClrType())
                              .AddMappingConfiguration(
                                  new EntityMappingConfiguration
                                      {
                                          MapInheritedProperties = true,
                                          TableName = new DatabaseName("E2")
                                      });
            modelConfiguration.Entity(entityType3.GetClrType())
                              .AddMappingConfiguration(
                                  new EntityMappingConfiguration
                                      {
                                          MapInheritedProperties = true,
                                          TableName = new DatabaseName("E3")
                                      });
            modelConfiguration.Configure(databaseMapping, ProviderRegistry.Sql2008_ProviderManifest);

            //Validate
            var entitySetMapping = databaseMapping.GetEntitySetMapping(model.GetEntitySet(entityType1));
            Assert.NotNull(entitySetMapping);
            Assert.Equal(3, entitySetMapping.EntityTypeMappings.Count());

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

            var table1 = entityType1Mapping.MappingFragments.Single().Table;
            var table2 = entityType2Mapping.MappingFragments.Single().Table;
            var table3 = entityType3Mapping.MappingFragments.Single().Table;

            Assert.False(entityType1Mapping.IsHierarchyMapping);
            Assert.Equal(2, table1.Properties.Count);
            Assert.Equal("P1", table1.Properties[0].Name);
            Assert.Equal("P2", table1.Properties[1].Name);
            Assert.Equal(2, entityType1Mapping.MappingFragments.Single().ColumnMappings.Count());
            Assert.Same(entityType1Mapping.MappingFragments.Single().ColumnMappings.ElementAt(0).ColumnProperty, table1.Properties[0]);
            Assert.Same(entityType1Mapping.MappingFragments.Single().ColumnMappings.ElementAt(1).ColumnProperty, table1.Properties[1]);

            Assert.False(entityType2Mapping.IsHierarchyMapping);
            Assert.Equal("E2", table2.Name);
            Assert.Equal(3, table2.Properties.Count);
            Assert.Equal("P1", table2.Properties[0].Name);
            Assert.Equal("P2", table2.Properties[1].Name);
            Assert.Equal("P3", table2.Properties[2].Name);
            Assert.NotSame(table1, table2);
            Assert.NotSame(table1.Properties[0], table2.Properties[0]);
            Assert.NotSame(table1.Properties[1], table2.Properties[1]);
            Assert.Equal(3, entityType2Mapping.MappingFragments.Single().ColumnMappings.Count());
            Assert.Same(entityType2Mapping.MappingFragments.Single().ColumnMappings.ElementAt(0).ColumnProperty, table2.Properties[0]);
            Assert.Same(entityType2Mapping.MappingFragments.Single().ColumnMappings.ElementAt(1).ColumnProperty, table2.Properties[1]);
            Assert.Same(entityType2Mapping.MappingFragments.Single().ColumnMappings.ElementAt(2).ColumnProperty, table2.Properties[2]);

            Assert.False(entityType3Mapping.IsHierarchyMapping);
            Assert.Equal("E3", table3.Name);
            Assert.Equal(3, table3.Properties.Count);
            Assert.Equal("P1", table3.Properties[0].Name);
            Assert.Equal("P2", table3.Properties[1].Name);
            Assert.Equal("P4", table3.Properties[2].Name);
            Assert.NotSame(table1, table3);
            Assert.NotSame(table3, table2);
            Assert.NotSame(table1.Properties[0], table3.Properties[0]);
            Assert.NotSame(table1.Properties[1], table3.Properties[1]);
            Assert.Equal(3, entityType3Mapping.MappingFragments.Single().ColumnMappings.Count());
            Assert.Same(entityType3Mapping.MappingFragments.Single().ColumnMappings.ElementAt(0).ColumnProperty, table3.Properties[0]);
            Assert.Same(entityType3Mapping.MappingFragments.Single().ColumnMappings.ElementAt(1).ColumnProperty, table3.Properties[1]);
            Assert.Same(entityType3Mapping.MappingFragments.Single().ColumnMappings.ElementAt(2).ColumnProperty, table3.Properties[2]);
        }
        public void Configure_should_configure_active_entities_and_complex_types()
        {
            var mockEntityType = new MockType();
            var mockComplexType = new MockType();

            var model = new EdmModel(DataSpace.CSpace);
            var entityType = model.AddEntityType("E");

            entityType.GetMetadataProperties().SetClrType(mockEntityType);
            var complexType = model.AddComplexType("C");

            complexType.GetMetadataProperties().SetClrType(mockComplexType);

            var modelConfiguration = new ModelConfiguration();
            var mockComplexTypeConfiguration = new Mock<ComplexTypeConfiguration>(mockComplexType.Object);
            var mockEntityTypeConfiguration = new Mock<EntityTypeConfiguration>(mockEntityType.Object);

            modelConfiguration.Add(mockComplexTypeConfiguration.Object);
            modelConfiguration.Add(mockEntityTypeConfiguration.Object);

            modelConfiguration.Configure(model);

            mockComplexTypeConfiguration.Verify(c => c.Configure(complexType));
            mockEntityTypeConfiguration.Verify(c => c.Configure(entityType, model));
        }
        public void Configure_mapping_can_configure_one_level_TPT_on_both_sides_of_tree()
        {
            //Setup
            var model = TestModelBuilderHelpers.CreateSingleLevelInheritanceWithThreeEntities();

            var entityType1 = model.GetEntityType("E1");
            var entityType2 = model.GetEntityType("E2");
            var entityType3 = model.GetEntityType("E3");
            (entityType1.GetDeclaredPrimitiveProperties().SingleOrDefault(p => p.Name == "P1")).SetStoreGeneratedPattern(
                StoreGeneratedPattern.Identity);

            var databaseMapping = new DatabaseMappingGenerator(ProviderRegistry.Sql2008_ProviderInfo, ProviderRegistry.Sql2008_ProviderManifest).Generate(model);
            var modelConfiguration = new ModelConfiguration();
            modelConfiguration.Entity(entityType2.GetClrType()).ToTable("E2");
            modelConfiguration.Entity(entityType3.GetClrType()).ToTable("E3");

            modelConfiguration.Configure(databaseMapping, ProviderRegistry.Sql2008_ProviderManifest);

            var entityTypeMapping1 = databaseMapping.GetEntityTypeMapping(entityType1);
            var entityTypeMapping2 = databaseMapping.GetEntityTypeMapping(entityType2);
            var entityTypeMapping3 = databaseMapping.GetEntityTypeMapping(entityType3);

            var table1 = entityTypeMapping1.MappingFragments.Single().Table;
            var table2 = entityTypeMapping2.MappingFragments.Single().Table;
            var table3 = entityTypeMapping3.MappingFragments.Single().Table;

            Assert.NotSame(table1, table2);
            Assert.NotSame(table1, table3);
            Assert.NotSame(table3, table2);

            Assert.True(entityTypeMapping1.IsHierarchyMapping);
            Assert.Equal(2, table1.Properties.Count);
            Assert.Equal("P1", table1.Properties[0].Name);
            Assert.Equal(StoreGeneratedPattern.Identity, table1.Properties[0].StoreGeneratedPattern);
            Assert.Equal("P2", table1.Properties[1].Name);
            Assert.Equal(2, entityTypeMapping1.MappingFragments.Single().ColumnMappings.Count());
            Assert.Equal("P1", entityTypeMapping1.MappingFragments.Single().ColumnMappings.ElementAt(0).ColumnProperty.Name);
            Assert.Equal("P2", entityTypeMapping1.MappingFragments.Single().ColumnMappings.ElementAt(1).ColumnProperty.Name);

            Assert.False(entityTypeMapping2.IsHierarchyMapping);
            Assert.Equal(2, entityTypeMapping2.MappingFragments.Single().ColumnMappings.Count());
            Assert.Equal("P1", entityTypeMapping2.MappingFragments.Single().ColumnMappings.ElementAt(0).ColumnProperty.Name);
            Assert.Equal(StoreGeneratedPattern.None, table2.Properties[0].StoreGeneratedPattern);
            Assert.Equal("P3", entityTypeMapping2.MappingFragments.Single().ColumnMappings.ElementAt(1).ColumnProperty.Name);
            Assert.Equal(2, table2.Properties.Count);
            Assert.Equal("P1", table2.Properties[0].Name);
            Assert.Equal("P3", table2.Properties[1].Name);
            Assert.NotSame(table1.Properties[0], table2.Properties[0]);

            Assert.False(entityTypeMapping3.IsHierarchyMapping);
            Assert.Equal(2, entityTypeMapping3.MappingFragments.Single().ColumnMappings.Count());
            Assert.Equal(StoreGeneratedPattern.None, table3.Properties[0].StoreGeneratedPattern);
            Assert.Equal("P1", entityTypeMapping3.MappingFragments.Single().ColumnMappings.ElementAt(0).ColumnProperty.Name);
            Assert.Equal("P4", entityTypeMapping3.MappingFragments.Single().ColumnMappings.ElementAt(1).ColumnProperty.Name);
            Assert.Equal(2, table3.Properties.Count);
            Assert.Equal("P1", table3.Properties[0].Name);
            Assert.Equal("P4", table3.Properties[1].Name);
            Assert.NotSame(table1.Properties[0], table3.Properties[0]);
            Assert.NotSame(table2.Properties[0], table3.Properties[0]);
        }
        public void Configure_should_not_throw_when_only_derived_type_mapped_to_function_but_base_is_abstract()
        {
            var modelConfiguration = new ModelConfiguration();

            var baseType = new MockType("B");
            var derivedType = new MockType("D").BaseType(baseType);

            modelConfiguration.Entity(baseType);
            modelConfiguration.Entity(derivedType).MapToStoredProcedures();

            var model = new EdmModel(DataSpace.CSpace);

            var baseEntity = model.AddEntityType("Base");
            baseEntity.Annotations.SetClrType(baseType);
            baseEntity.Abstract = true;

            var derivedEntity = model.AddEntityType("Derived");
            derivedEntity.Annotations.SetClrType(derivedType);
            derivedEntity.BaseType = baseEntity;

            modelConfiguration.Configure(model);

            Assert.True(modelConfiguration.Entity(baseType).IsMappedToFunctions);
            Assert.True(modelConfiguration.Entity(derivedType).IsMappedToFunctions);
        }
        public void Configure_should_configure_default_schema()
        {
            var modelConfiguration
                = new ModelConfiguration
                      {
                          DefaultSchema = "foo"
                      };

            var databaseMetadata = new EdmModel(DataSpace.CSpace);
            databaseMetadata.AddEntitySet("ES", new EntityType("E", "N", DataSpace.CSpace));

            var databaseMapping
                = new DbDatabaseMapping().Initialize(
                    new EdmModel(DataSpace.CSpace),
                    databaseMetadata);

            modelConfiguration.Configure(databaseMapping, ProviderRegistry.Sql2008_ProviderManifest);

            Assert.Equal("foo", databaseMapping.Database.GetEntitySets().Single().Schema);
        }
        public void Configure_should_not_throw_when_only_derived_type_mapped_to_function_but_base_is_abstract()
        {
            var modelConfiguration = new ModelConfiguration();

            modelConfiguration.Entity(typeof(BType2));
            modelConfiguration.Entity(typeof(CType2)).MapToStoredProcedures();

            var model = new EdmModel(DataSpace.CSpace);

            var baseEntity = model.AddEntityType("Base");
            baseEntity.GetMetadataProperties().SetClrType(typeof(BType2));
            baseEntity.Abstract = true;

            var derivedEntity = model.AddEntityType("Derived");
            derivedEntity.GetMetadataProperties().SetClrType(typeof(CType2));
            derivedEntity.BaseType = baseEntity;

            modelConfiguration.Configure(model);

            Assert.NotNull(modelConfiguration.Entity(typeof(BType2)).ModificationStoredProceduresConfiguration);
            Assert.NotNull(modelConfiguration.Entity(typeof(CType2)).ModificationStoredProceduresConfiguration);
        }
        public void Configure_should_uniquify_unconfigured_assocation_function_names()
        {
            var typeA = new MockType("A");
            var typeB = new MockType("B").Property(typeA.AsCollection(), "As");
            var mockPropertyInfo = typeB.GetProperty("As");
            typeA.Property(typeB.AsCollection(), "Bs");

            var modelConfiguration = new ModelConfiguration();

            var navigationPropertyConfiguration
                = modelConfiguration.Entity(typeB).Navigation(mockPropertyInfo);

            navigationPropertyConfiguration.ModificationFunctionsConfiguration
                = new ModificationFunctionsConfiguration();

            var modificationFunctionConfiguration = new ModificationFunctionConfiguration();
            modificationFunctionConfiguration.HasName("M2M_Delete");

            navigationPropertyConfiguration.ModificationFunctionsConfiguration
                .Insert(modificationFunctionConfiguration);

            var model = new EdmModel(DataSpace.CSpace);

            var entityA = model.AddEntityType("A");
            entityA.Annotations.SetClrType(typeA);
            entityA.SetConfiguration(modelConfiguration.Entity(typeA));

            var entityB = model.AddEntityType("B");
            entityB.Annotations.SetClrType(typeB);
            entityB.SetConfiguration(modelConfiguration.Entity(typeB));

            model.AddEntitySet("AS", entityA);
            model.AddEntitySet("BS", entityB);

            var associationType
                = model.AddAssociationType(
                    "M2M",
                    entityA,
                    RelationshipMultiplicity.Many,
                    entityB,
                    RelationshipMultiplicity.Many);

            associationType.SetConfiguration(navigationPropertyConfiguration);

            var navigationProperty
                = entityB.AddNavigationProperty("As", associationType);

            navigationProperty.SetClrPropertyInfo(mockPropertyInfo);

            model.AddAssociationSet("M2MSet", associationType);

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

            modelConfiguration.Configure(databaseMapping, ProviderRegistry.Sql2008_ProviderManifest);

            Assert.True(databaseMapping.Database.Functions.Any(f => f.Name == "M2M_Delete"));
            Assert.True(databaseMapping.Database.Functions.Any(f => f.Name == "M2M_Delete1"));
        }
        public void Configure_should_throw_when_only_derived_type_mapped_to_functions()
        {
            var modelConfiguration = new ModelConfiguration();

            modelConfiguration.Entity(typeof(BType2));
            modelConfiguration.Entity(typeof(AType2)).MapToStoredProcedures();

            var model = new EdmModel(DataSpace.CSpace);

            var baseEntity = model.AddEntityType("Base");
            baseEntity.GetMetadataProperties().SetClrType(typeof(BType2));

            var derivedEntity = model.AddEntityType("Derived");
            derivedEntity.GetMetadataProperties().SetClrType(typeof(AType2));
            derivedEntity.BaseType = baseEntity;

            Assert.Equal(
                Strings.BaseTypeNotMappedToFunctions("BType2", "AType2"),
                Assert.Throws<InvalidOperationException>(
                    () => modelConfiguration.Configure(model)).Message);
        }
        public void Configure_entity_splitting_should_throw_if_ignored_property_is_mapped()
        {
            EdmModel model =
                new TestModelBuilder()
                    .Entity("E")
                    .Key("P1");
            var databaseMapping = new DatabaseMappingGenerator(ProviderRegistry.Sql2008_ProviderInfo, ProviderRegistry.Sql2008_ProviderManifest).Generate(model);

            var entityType = model.GetEntityType("E");
            var modelConfiguration = new ModelConfiguration();
            var entityConfiguration = modelConfiguration.Entity(entityType.GetClrType());
            var p1PropertyInfo = entityType.GetDeclaredPrimitiveProperties().SingleOrDefault(p => p.Name == "P1").GetClrPropertyInfo();
            var entityMappingConfiguration1 =
                new EntityMappingConfiguration
                    {
                        Properties = new List<PropertyPath>
                                         {
                                             new PropertyPath(p1PropertyInfo),
                                             new PropertyPath(new MockPropertyInfo(typeof(int), "P2"))
                                         },
                        TableName = new DatabaseName("E")
                    };
            entityConfiguration.AddMappingConfiguration(entityMappingConfiguration1);

            Assert.Equal(
                Strings.EntityMappingConfiguration_CannotMapIgnoredProperty("E", "P2"),
                Assert.Throws<InvalidOperationException>(
                    () => modelConfiguration.Configure(databaseMapping, ProviderRegistry.Sql2008_ProviderManifest)).Message);
        }
        public void Configure_when_base_entity_mapped_to_function_should_map_sub_types_to_functions()
        {
            var modelConfiguration = new ModelConfiguration();

            modelConfiguration.Entity(typeof(CType2)).MapToStoredProcedures();
            modelConfiguration.Entity(typeof(BType2));
            modelConfiguration.Entity(typeof(AType2));

            var model = new EdmModel(DataSpace.CSpace);

            var rootEntity = model.AddEntityType("Root");
            rootEntity.GetMetadataProperties().SetClrType(typeof(CType2));

            var middleEntity = model.AddEntityType("Middle");
            middleEntity.GetMetadataProperties().SetClrType(typeof(BType2));
            middleEntity.BaseType = rootEntity;

            var leafEntity = model.AddEntityType("Leaf");
            leafEntity.GetMetadataProperties().SetClrType(typeof(AType2));
            leafEntity.BaseType = middleEntity;

            modelConfiguration.Configure(model);

            Assert.NotNull(modelConfiguration.Entity(typeof(CType2)).ModificationStoredProceduresConfiguration);
            Assert.NotNull(modelConfiguration.Entity(typeof(BType2)).ModificationStoredProceduresConfiguration);
            Assert.NotNull(modelConfiguration.Entity(typeof(AType2)).ModificationStoredProceduresConfiguration);
        }
        public void Configure_mapping_can_process_entity_splitting()
        {
            EdmModel model =
                new TestModelBuilder()
                    .Entity("E")
                    .Key("P1")
                    .Property("P2")
                    .Property("P3")
                    .Property("P4")
                    .Property("P5");
            var databaseMapping = new DatabaseMappingGenerator(ProviderRegistry.Sql2008_ProviderInfo, ProviderRegistry.Sql2008_ProviderManifest).Generate(model);

            var entityType = model.GetEntityType("E");
            var modelConfiguration = new ModelConfiguration();
            var entityMappingConfiguration1 =
                new EntityMappingConfiguration
                    {
                        Properties =
                            new List<PropertyPath>
                                {
                                    new PropertyPath(
                                        entityType.GetDeclaredPrimitiveProperties().SingleOrDefault(p => p.Name == "P1").GetClrPropertyInfo(
                                            
                                        )),
                                    new PropertyPath(
                                        entityType.GetDeclaredPrimitiveProperties().SingleOrDefault(p => p.Name == "P2").GetClrPropertyInfo(
                                            
                                        )),
                                    new PropertyPath(
                                        entityType.GetDeclaredPrimitiveProperties().SingleOrDefault(p => p.Name == "P3").GetClrPropertyInfo(
                                            
                                        ))
                                },
                        TableName = new DatabaseName("E1")
                    };
            var entityMappingConfiguration2 =
                new EntityMappingConfiguration
                    {
                        Properties =
                            new List<PropertyPath>
                                {
                                    new PropertyPath(
                                        entityType.GetDeclaredPrimitiveProperties().SingleOrDefault(p => p.Name == "P1").GetClrPropertyInfo(
                                            
                                        )),
                                    new PropertyPath(
                                        entityType.GetDeclaredPrimitiveProperties().SingleOrDefault(p => p.Name == "P4").GetClrPropertyInfo(
                                            
                                        )),
                                    new PropertyPath(
                                        entityType.GetDeclaredPrimitiveProperties().SingleOrDefault(p => p.Name == "P5").GetClrPropertyInfo(
                                            
                                        ))
                                },
                        TableName = new DatabaseName("E2")
                    };
            var entityConfiguration = modelConfiguration.Entity(entityType.GetClrType());
            entityConfiguration.AddMappingConfiguration(entityMappingConfiguration1);
            entityConfiguration.AddMappingConfiguration(entityMappingConfiguration2);
            modelConfiguration.Configure(databaseMapping, ProviderRegistry.Sql2008_ProviderManifest);

            var entityTypeMapping = databaseMapping.GetEntityTypeMapping(entityType);
            var table1 = entityTypeMapping.MappingFragments[0].Table;
            var table2 = entityTypeMapping.MappingFragments[1].Table;
            Assert.NotSame(table1, table2);
            Assert.Equal("E1", table1.GetTableName().Name);
            Assert.Equal("E2", table2.GetTableName().Name);
            Assert.Equal(3, table1.Properties.Count);
            Assert.Equal(3, table2.Properties.Count);
            Assert.Equal(2, entityTypeMapping.MappingFragments.Count);
            var entityTypeMappingFragment1 = entityTypeMapping.MappingFragments[0];
            var entityTypeMappingFragment2 = entityTypeMapping.MappingFragments[1];
            Assert.Equal(3, entityTypeMappingFragment1.ColumnMappings.Count());
            Assert.Equal("P1", entityTypeMappingFragment1.ColumnMappings.ElementAt(0).ColumnProperty.Name);
            Assert.Equal("P2", entityTypeMappingFragment1.ColumnMappings.ElementAt(1).ColumnProperty.Name);
            Assert.Equal("P3", entityTypeMappingFragment1.ColumnMappings.ElementAt(2).ColumnProperty.Name);
            Assert.Equal(3, entityTypeMappingFragment2.ColumnMappings.Count());
            Assert.Equal("P1", entityTypeMappingFragment2.ColumnMappings.ElementAt(0).ColumnProperty.Name);
            Assert.Equal("P4", entityTypeMappingFragment2.ColumnMappings.ElementAt(1).ColumnProperty.Name);
            Assert.Equal("P5", entityTypeMappingFragment2.ColumnMappings.ElementAt(2).ColumnProperty.Name);
        }
        public void Configure_mapping_can_process_one_level_TPH_on_both_sides_of_tree()
        {
            //Setup
            var model = TestModelBuilderHelpers.CreateSingleLevelInheritanceWithThreeEntities();
            var databaseMapping = new DatabaseMappingGenerator(ProviderRegistry.Sql2008_ProviderInfo, ProviderRegistry.Sql2008_ProviderManifest).Generate(model);

            var entityType1 = model.GetEntityType("E1");
            var entityType2 = model.GetEntityType("E2");
            var entityType3 = model.GetEntityType("E3");

            // Action
            var modelConfiguration = new ModelConfiguration();
            var entity1Configuration = modelConfiguration.Entity(entityType1.GetClrType());
            var entity1MappingConfiguration =
                new EntityMappingConfiguration
                    {
                        TableName = new DatabaseName("E1")
                    };
            entity1MappingConfiguration
                .AddValueCondition(
                    new ValueConditionConfiguration(entity1MappingConfiguration, "P3")
                        {
                            Value = null
                        });
            entity1MappingConfiguration
                .AddValueCondition(
                    new ValueConditionConfiguration(entity1MappingConfiguration, "P4")
                        {
                            Value = null
                        });
            entity1Configuration.AddMappingConfiguration(entity1MappingConfiguration);
            var entity1SubTypeMappingConfiguration =
                new EntityMappingConfiguration
                    {
                        TableName = new DatabaseName("E1")
                    };
            entity1SubTypeMappingConfiguration
                .AddValueCondition(
                    new ValueConditionConfiguration(entity1SubTypeMappingConfiguration, "P3")
                        {
                            Value = null
                        });
            entity1SubTypeMappingConfiguration
                .AddNullabilityCondition(
                    new NotNullConditionConfiguration(
                        entity1SubTypeMappingConfiguration,
                        new PropertyPath(
                            entityType3.GetDeclaredPrimitiveProperties().SingleOrDefault(p => p.Name == "P4").GetClrPropertyInfo())));
            entity1Configuration.AddSubTypeMappingConfiguration(entityType3.GetClrType(), entity1SubTypeMappingConfiguration);
            var entity2Configuration = modelConfiguration.Entity(entityType2.GetClrType());
            var entity2MappingConfiguration =
                new EntityMappingConfiguration
                    {
                        TableName = new DatabaseName("E1")
                    };
            entity2MappingConfiguration
                .AddNullabilityCondition(
                    new NotNullConditionConfiguration(
                        entity2MappingConfiguration,
                        new PropertyPath(
                            entityType2.GetDeclaredPrimitiveProperties().SingleOrDefault(p => p.Name == "P3").GetClrPropertyInfo())));
            entity2MappingConfiguration
                .AddValueCondition(
                    new ValueConditionConfiguration(entity2MappingConfiguration, "P4")
                        {
                            Value = null
                        });
            entity2Configuration.AddMappingConfiguration(entity2MappingConfiguration);
            modelConfiguration.NormalizeConfigurations();
            modelConfiguration.Configure(databaseMapping, ProviderRegistry.Sql2008_ProviderManifest);

            //Validate
            var entitySetMapping = databaseMapping.GetEntitySetMapping(model.GetEntitySet(entityType1));
            Assert.NotNull(entitySetMapping);
            Assert.Equal(4, entitySetMapping.EntityTypeMappings.Count());

            var entityType1Mapping = databaseMapping.GetEntityTypeMapping(entityType1);
            var entityType1MappingConditions = databaseMapping.GetEntityTypeMappings(entityType1).Single(x => !x.IsHierarchyMapping);
            var entityType2Mapping = databaseMapping.GetEntityTypeMapping(entityType2);
            var entityType3Mapping = databaseMapping.GetEntityTypeMapping(entityType3);

            var table1 = entityType1Mapping.MappingFragments.Single().Table;
            var table2 = entityType2Mapping.MappingFragments.Single().Table;
            var table3 = entityType3Mapping.MappingFragments.Single().Table;

            Assert.True(entityType1Mapping.IsHierarchyMapping);
            Assert.Equal(4, table1.Properties.Count);
            Assert.Equal("P1", table1.Properties[0].Name);
            Assert.Equal("P2", table1.Properties[1].Name);
            Assert.Equal("P3", table1.Properties[2].Name);
            Assert.Equal("P4", table1.Properties[3].Name);
            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.Same(
                table1.Properties[2], entityType1MappingConditions.MappingFragments.Single().ColumnConditions.ElementAt(0).Column);
            Assert.True((bool)entityType1MappingConditions.MappingFragments.Single().ColumnConditions.ElementAt(0).IsNull);
            Assert.Same(
                table1.Properties[3], entityType1MappingConditions.MappingFragments.Single().ColumnConditions.ElementAt(1).Column);
            Assert.True((bool)entityType1MappingConditions.MappingFragments.Single().ColumnConditions.ElementAt(1).IsNull);

            Assert.False(entityType2Mapping.IsHierarchyMapping);
            Assert.Same(table1, table2);
            Assert.Same(table1.Properties[0], table2.Properties[0]);
            Assert.Same(table1.Properties[1], table2.Properties[1]);
            Assert.Equal(2, entityType2Mapping.MappingFragments.Single().ColumnMappings.Count());
            Assert.Equal("P1", entityType2Mapping.MappingFragments.Single().ColumnMappings.ElementAt(0).ColumnProperty.Name);
            Assert.Equal("P3", entityType2Mapping.MappingFragments.Single().ColumnMappings.ElementAt(1).ColumnProperty.Name);
            Assert.Same(table1.Properties[3], entityType2Mapping.MappingFragments.Single().ColumnConditions.ElementAt(0).Column);
            Assert.True((bool)entityType2Mapping.MappingFragments.Single().ColumnConditions.ElementAt(0).IsNull);
            Assert.Same(table1.Properties[2], entityType2Mapping.MappingFragments.Single().ColumnConditions.ElementAt(1).Column);
            Assert.False((bool)entityType2Mapping.MappingFragments.Single().ColumnConditions.ElementAt(1).IsNull);

            Assert.False(entityType3Mapping.IsHierarchyMapping);
            Assert.Same(table1, table3);
            Assert.Same(table1.Properties[0], table3.Properties[0]);
            Assert.Same(table1.Properties[1], table3.Properties[1]);
            Assert.Equal(2, entityType3Mapping.MappingFragments.Single().ColumnMappings.Count());
            Assert.Equal("P1", entityType3Mapping.MappingFragments.Single().ColumnMappings.ElementAt(0).ColumnProperty.Name);
            Assert.Equal("P4", entityType3Mapping.MappingFragments.Single().ColumnMappings.ElementAt(1).ColumnProperty.Name);
            Assert.Same(table1.Properties[2], entityType3Mapping.MappingFragments.Single().ColumnConditions.ElementAt(0).Column);
            Assert.True((bool)entityType3Mapping.MappingFragments.Single().ColumnConditions.ElementAt(0).IsNull);
            Assert.Same(table1.Properties[3], entityType3Mapping.MappingFragments.Single().ColumnConditions.ElementAt(1).Column);
            Assert.False((bool)entityType3Mapping.MappingFragments.Single().ColumnConditions.ElementAt(1).IsNull);
        }
        public void Configure_should_configure_default_schema_on_functions()
        {
            var modelConfiguration
                = new ModelConfiguration
                      {
                          DefaultSchema = "foo"
                      };

            var databaseMetadata = new EdmModel(DataSpace.SSpace);
            databaseMetadata.AddFunction("F", new EdmFunctionPayload());

            var databaseMapping
                = new DbDatabaseMapping().Initialize(
                    new EdmModel(DataSpace.CSpace),
                    databaseMetadata);

            modelConfiguration.Configure(databaseMapping, ProviderRegistry.Sql2008_ProviderManifest);

            Assert.Equal("foo", databaseMapping.Database.Functions.Single().Schema);
        }
        public void Configure_when_base_entity_mapped_to_function_should_map_sub_types_to_functions()
        {
            var modelConfiguration = new ModelConfiguration();

            var rootType = new MockType();
            var middleType = new MockType().BaseType(rootType);
            var leafType = new MockType().BaseType(middleType);

            modelConfiguration.Entity(rootType).MapToStoredProcedures();
            modelConfiguration.Entity(middleType);
            modelConfiguration.Entity(leafType);

            var model = new EdmModel(DataSpace.CSpace);

            var rootEntity = model.AddEntityType("Root");
            rootEntity.Annotations.SetClrType(rootType);

            var middleEntity = model.AddEntityType("Middle");
            middleEntity.Annotations.SetClrType(middleType);
            middleEntity.BaseType = rootEntity;

            var leafEntity = model.AddEntityType("Leaf");
            leafEntity.Annotations.SetClrType(leafType);
            leafEntity.BaseType = middleEntity;

            modelConfiguration.Configure(model);

            Assert.True(modelConfiguration.Entity(rootType).IsMappedToFunctions);
            Assert.True(modelConfiguration.Entity(middleType).IsMappedToFunctions);
            Assert.True(modelConfiguration.Entity(leafType).IsMappedToFunctions);
        }