public void Configure_should_update_IsRowVersion()
        {
            var configuration = CreateConfiguration();
            configuration.IsRowVersion = true;
            var property = new EdmProperty().AsPrimitive();

            configuration.Configure(property);

            Assert.Equal(8, property.PropertyType.PrimitiveTypeFacets.MaxLength);
            Assert.Equal(false, property.PropertyType.IsNullable);
            Assert.Equal(EdmConcurrencyMode.Fixed, property.ConcurrencyMode);
            Assert.Equal(DbStoreGeneratedPattern.Computed, property.GetStoreGeneratedPattern());

            var edmPropertyMapping = new DbEdmPropertyMapping { Column = new DbTableColumnMetadata { Facets = new DbPrimitiveTypeFacets() } };

            configuration.Configure(new[] { Tuple.Create(edmPropertyMapping, new DbTableMetadata()) }, ProviderRegistry.Sql2008_ProviderManifest);
            Assert.Equal("rowversion", edmPropertyMapping.Column.TypeName);
        }
        public void GetPropertyMapping_should_return_mapping_with_path()
        {
            var entityTypeMapping = new DbEntityTypeMapping();
            var propertyFoo = new EdmProperty { Name = "Foo" };
            var propertyBar = new EdmProperty { Name = "Bar" };
            var entityPropertyMapping = new DbEdmPropertyMapping
                {
                    PropertyPath = new[]
                        {
                            propertyFoo,
                            propertyBar,
                        }
                };

            var entityTypeMappingFragment = new DbEntityTypeMappingFragment();
            entityTypeMappingFragment.PropertyMappings.Add(entityPropertyMapping);
            entityTypeMapping.TypeMappingFragments.Add(entityTypeMappingFragment);

            Assert.Same(entityPropertyMapping, entityTypeMapping.GetPropertyMapping(propertyFoo, propertyBar));
        }
        public void GetComplexPropertyMappings_should_return_all_complex_property_mappings_for_type()
        {
            var databaseMapping = new DbDatabaseMapping()
                .Initialize(new EdmModel().Initialize(), new DbDatabaseMetadata());
            var entitySet = new EdmEntitySet();
            var entitySetMapping = databaseMapping.AddEntitySetMapping(entitySet);
            var entityTypeMapping = new DbEntityTypeMapping();
            entitySetMapping.EntityTypeMappings.Add(entityTypeMapping);
            var entityTypeMappingFragment = new DbEntityTypeMappingFragment();
            entityTypeMapping.TypeMappingFragments.Add(entityTypeMappingFragment);
            var propertyMapping1 = new DbEdmPropertyMapping();
            var complexType = new EdmComplexType();
            complexType.SetClrType(typeof(object));
            propertyMapping1.PropertyPath.Add(new EdmProperty { PropertyType = new EdmTypeReference { EdmType = complexType } });
            entityTypeMappingFragment.PropertyMappings.Add(propertyMapping1);
            var propertyMapping2 = new DbEdmPropertyMapping();
            propertyMapping2.PropertyPath.Add(new EdmProperty { PropertyType = new EdmTypeReference() });
            propertyMapping2.PropertyPath.Add(new EdmProperty { PropertyType = new EdmTypeReference { EdmType = complexType } });
            entityTypeMappingFragment.PropertyMappings.Add(propertyMapping2);

            Assert.Equal(2, databaseMapping.GetComplexPropertyMappings(typeof(object)).Count());
        }
        public void Configure_should_update_mapped_column_type()
        {
            var configuration = CreateConfiguration();
            configuration.ColumnType = "Foo";

            var edmPropertyMapping = new DbEdmPropertyMapping { Column = new DbTableColumnMetadata() };

            configuration.Configure(new[] { Tuple.Create(edmPropertyMapping, new DbTableMetadata()) }, ProviderRegistry.Sql2008_ProviderManifest);

            Assert.Equal("Foo", edmPropertyMapping.Column.TypeName);
        }
        public void Configure_should_update_mapped_column_order()
        {
            var configuration = CreateConfiguration();
            configuration.ColumnOrder = 2;
            configuration.ColumnType = "nvarchar";

            var edmPropertyMapping = new DbEdmPropertyMapping { Column = new DbTableColumnMetadata { Facets = new DbPrimitiveTypeFacets() } };

            configuration.Configure(new[] { Tuple.Create(edmPropertyMapping, new DbTableMetadata()) }, ProviderRegistry.Sql2008_ProviderManifest);

            Assert.Equal(2, edmPropertyMapping.Column.GetOrder());
        }
        public void Configure_should_merge_SSpace_configurations()
        {
            var configurationA = CreateConfiguration();
            configurationA.ColumnName = "foo";
            var configurationB = CreateConfiguration();
            configurationB.ColumnType = "nvarchar";

            var edmPropertyMapping = new DbEdmPropertyMapping { Column = new DbTableColumnMetadata { Facets = new DbPrimitiveTypeFacets() } };

            configurationA.Configure(new[] { Tuple.Create(edmPropertyMapping, new DbTableMetadata()) }, ProviderRegistry.Sql2008_ProviderManifest);

            Assert.Equal("foo", ((PrimitivePropertyConfiguration)edmPropertyMapping.Column.GetConfiguration()).ColumnName);

            configurationB.Configure(new[] { Tuple.Create(edmPropertyMapping, new DbTableMetadata()) }, ProviderRegistry.Sql2008_ProviderManifest);

            Assert.Equal("foo", ((PrimitivePropertyConfiguration)edmPropertyMapping.Column.GetConfiguration()).ColumnName);
            Assert.Equal("nvarchar", ((PrimitivePropertyConfiguration)edmPropertyMapping.Column.GetConfiguration()).ColumnType);
        }
        public void Configure_should_set_SSpace_configuration_annotation()
        {
            var configuration = CreateConfiguration();

            var edmPropertyMapping = new DbEdmPropertyMapping { Column = new DbTableColumnMetadata() };

            Assert.Null(edmPropertyMapping.Column.GetConfiguration());

            configuration.Configure(new[] { Tuple.Create(edmPropertyMapping, new DbTableMetadata()) }, ProviderRegistry.Sql2008_ProviderManifest);

            Assert.Same(configuration, edmPropertyMapping.Column.GetConfiguration());
        }
        /// <summary>
        ///     Makes sure only the required property mappings are present
        /// </summary>
        private static void ConfigureTypeMappings(
            TableMapping tableMapping,
            Dictionary<EdmEntityType, DbEntityTypeMapping> rootMappings,
            EdmEntityType entityType,
            DbEntityTypeMappingFragment propertiesTypeMappingFragment,
            DbEntityTypeMappingFragment conditionTypeMappingFragment)
        {
            var existingPropertyMappings =
                new List<DbEdmPropertyMapping>(
                    propertiesTypeMappingFragment.PropertyMappings.Where(pm => !pm.Column.IsPrimaryKeyColumn));
            var existingConditions = new List<DbColumnCondition>(propertiesTypeMappingFragment.ColumnConditions);

            foreach (var columnMapping in from cm in tableMapping.ColumnMappings
                                          from pm in cm.PropertyMappings
                                          where pm.EntityType == entityType
                                          select new
                                                     {
                                                         cm.Column,
                                                         Property = pm
                                                     })
            {
                if (columnMapping.Property.PropertyPath != null
                    &&
                    !IsRootTypeMapping(
                        rootMappings, columnMapping.Property.EntityType, columnMapping.Property.PropertyPath))
                {
                    var existingPropertyMapping =
                        propertiesTypeMappingFragment.PropertyMappings.SingleOrDefault(
                            x => x.PropertyPath == columnMapping.Property.PropertyPath);
                    if (existingPropertyMapping != null)
                    {
                        existingPropertyMappings.Remove(existingPropertyMapping);
                    }
                    else
                    {
                        existingPropertyMapping = new DbEdmPropertyMapping
                                                      {
                                                          Column = columnMapping.Column,
                                                          PropertyPath = columnMapping.Property.PropertyPath
                                                      };
                        propertiesTypeMappingFragment.PropertyMappings.Add(existingPropertyMapping);
                    }
                }

                if (columnMapping.Property.Conditions != null)
                {
                    foreach (var condition in columnMapping.Property.Conditions)
                    {
                        if (conditionTypeMappingFragment.ColumnConditions.Contains(condition))
                        {
                            existingConditions.Remove(condition);
                        }
                        else if (!entityType.IsAbstract)
                        {
                            conditionTypeMappingFragment.ColumnConditions.Add(condition);
                        }
                    }
                }
            }

            // Any leftover mappings are removed
            foreach (var leftoverPropertyMapping in existingPropertyMappings)
            {
                propertiesTypeMappingFragment.PropertyMappings.Remove(leftoverPropertyMapping);
            }

            foreach (var leftoverCondition in existingConditions)
            {
                conditionTypeMappingFragment.ColumnConditions.Remove(leftoverCondition);
            }

            if (entityType.IsAbstract)
            {
                propertiesTypeMappingFragment.ColumnConditions.Clear();
            }
        }
        public static void CopyPropertyMappingToFragment(
            DbEdmPropertyMapping propertyMapping, DbEntityTypeMappingFragment fragment, bool useExisting)
        {
            // Ensure column is in the fragment's table
            var column = TablePrimitiveOperations.IncludeColumn(fragment.Table, propertyMapping.Column, useExisting);

            // Add the property mapping
            fragment.PropertyMappings.Add(
                new DbEdmPropertyMapping
                    {
                        PropertyPath = propertyMapping.PropertyPath,
                        Column = column
                    });
        }
        public static void MovePropertyMapping(
            DbDatabaseMetadata database,
            DbEntityTypeMappingFragment fromFragment,
            DbEntityTypeMappingFragment toFragment,
            DbEdmPropertyMapping propertyMapping,
            bool requiresUpdate,
            bool useExisting)
        {
            // move the column from the formTable to the table in fragment
            if (requiresUpdate && fromFragment.Table != toFragment.Table)
            {
                UpdatePropertyMapping(database, propertyMapping, fromFragment.Table, toFragment.Table, useExisting);
            }

            // move the propertyMapping
            fromFragment.PropertyMappings.Remove(propertyMapping);
            toFragment.PropertyMappings.Add(propertyMapping);
        }
 private static void UpdatePropertyMapping(
     DbDatabaseMetadata database,
     DbEdmPropertyMapping propertyMapping,
     DbTableMetadata fromTable,
     DbTableMetadata toTable,
     bool useExisting)
 {
     propertyMapping.Column
         = TableOperations.CopyColumnAndAnyConstraints(
             database, fromTable, toTable, propertyMapping.Column, useExisting, false);
     propertyMapping.SyncNullabilityCSSpace();
 }
        private static DbTableMetadata FindBaseTableForExtraPropertyMapping(
            DbDatabaseMapping databaseMapping, EdmEntityType entityType, DbEdmPropertyMapping pm)
        {
            var baseType = entityType.BaseType;

            DbEntityTypeMappingFragment baseFragment = null;

            while (baseType != null
                   && baseFragment == null)
            {
                var baseMapping = databaseMapping.GetEntityTypeMapping(baseType);
                if (baseMapping != null)
                {
                    baseFragment =
                        baseMapping.TypeMappingFragments.SingleOrDefault(
                            f => f.PropertyMappings.Any(bpm => bpm.PropertyPath.SequenceEqual(pm.PropertyPath)));

                    if (baseFragment != null)
                    {
                        return baseFragment.Table;
                    }
                }
                baseType = baseType.BaseType;
            }
            return null;
        }
        private static DbTableMetadata FindTableForExtraPropertyMapping(
            DbDatabaseMapping databaseMapping,
            EdmEntityType entityType,
            DbTableMetadata fromTable,
            DbTableMetadata toTable,
            ref DbTableMetadata unmappedTable,
            DbEdmPropertyMapping pm)
        {
            var extraTable = FindBaseTableForExtraPropertyMapping(databaseMapping, entityType, pm);

            if (extraTable == null)
            {
                if (fromTable != toTable
                    && entityType.BaseType == null)
                {
                    return fromTable;
                }

                if (unmappedTable == null)
                {
                    unmappedTable = databaseMapping.Database.AddTable(fromTable.Name, fromTable);
                }
                extraTable = unmappedTable;
            }

            return extraTable;
        }
 private static DbTableMetadata FindTableForTemporaryExtraPropertyMapping(
     DbDatabaseMapping databaseMapping,
     EdmEntityType entityType,
     DbTableMetadata fromTable,
     DbTableMetadata toTable,
     DbEdmPropertyMapping pm)
 {
     var extraTable = fromTable;
     if (fromTable == toTable)
     {
         extraTable = databaseMapping.Database.AddTable(entityType.Name, fromTable);
     }
     else if (entityType.BaseType == null)
     {
         extraTable = fromTable;
     }
     else
     {
         // find where the base mappings are and put them in that table
         extraTable = FindBaseTableForExtraPropertyMapping(databaseMapping, entityType, pm);
         if (extraTable == null)
         {
             extraTable = fromTable;
         }
     }
     return extraTable;
 }