public void AddEntityTypeMappingFragment(
            EdmEntitySet entitySet, EdmEntityType entityType, DbEntityTypeMappingFragment fragment)
        {
            Contract.Assert(fragment.Table == Table);

            _entityTypes.Add(entitySet, entityType);

            var defaultDiscriminatorColumn = fragment.GetDefaultDiscriminator();
            DbColumnCondition defaultDiscriminatorCondition = null;
            if (defaultDiscriminatorColumn != null)
            {
                defaultDiscriminatorCondition =
                    fragment.ColumnConditions.SingleOrDefault(cc => cc.Column == defaultDiscriminatorColumn);
            }

            foreach (var pm in fragment.PropertyMappings)
            {
                var columnMapping = FindOrCreateColumnMapping(pm.Column);
                columnMapping.AddMapping(
                    entityType,
                    pm.PropertyPath,
                    fragment.ColumnConditions.Where(cc => cc.Column == pm.Column),
                    defaultDiscriminatorColumn == pm.Column);
            }

            // Add any column conditions that aren't mapped to properties
            foreach (
                var cc in
                    fragment.ColumnConditions.Where(cc => !fragment.PropertyMappings.Any(pm => pm.Column == cc.Column)))
            {
                var columnMapping = FindOrCreateColumnMapping(cc.Column);
                columnMapping.AddMapping(entityType, null, new[] { cc }, defaultDiscriminatorColumn == cc.Column);
            }
        }
        public static void SetKeyNamesType(this DbTableMetadata table, EdmEntityType entityType)
        {
            //Contract.Requires(table != null);
            //Contract.Requires(entityType != null);

            table.Annotations.SetAnnotation(KeyNamesTypeAnnotation, entityType);
        }
        public void Add(EdmEntitySet entitySet, EdmEntityType entityType)
        {
            Contract.Requires(entitySet != null);
            Contract.Requires(entityType != null);

            var i = 0;

            List<EdmEntityType> entityTypes;
            if (!_entityTypes.TryGetValue(entitySet, out entityTypes))
            {
                entityTypes = new List<EdmEntityType>();
                _entityTypes.Add(entitySet, entityTypes);
            }

            for (; i < entityTypes.Count; i++)
            {
                if (entityTypes[i] == entityType)
                {
                    return;
                }
                else if (entityType.IsAncestorOf(entityTypes[i]))
                {
                    break;
                }
            }
            entityTypes.Insert(i, entityType);
        }
        public void Apply_should_move_declared_keys_head_of_declared_properties_list()
        {
            var entityType = new EdmEntityType();
            entityType.SetClrType(typeof(SimpleEntity));
            entityType.AddPrimitiveProperty("StaticProperty");
            entityType.AddPrimitiveProperty("PrivateProperty");
            entityType.AddPrimitiveProperty("InheritedPropertyB");
            entityType.AddPrimitiveProperty("InheritedPropertyA");
            entityType.AddPrimitiveProperty("PropertyB");
            entityType.AddPrimitiveProperty("PropertyA");
            entityType.DeclaredKeyProperties.Add(
                entityType.AddPrimitiveProperty("Key"));

            ((IEdmConvention<EdmEntityType>)new DeclaredPropertyOrderingConvention())
                .Apply(entityType, new EdmModel());

            Assert.True(
                entityType.DeclaredProperties.Select(e => e.Name)
                    .SequenceEqual(
                        new[]
                            {
                                "Key", "PrivateProperty", "PropertyA", "PropertyB", "InheritedPropertyA", "InheritedPropertyB",
                                "StaticProperty"
                            }));
        }
        public bool Contains(EdmEntitySet entitySet, EdmEntityType entityType)
        {
            Contract.Requires(entitySet != null);
            Contract.Requires(entityType != null);

            List<EdmEntityType> setTypes;
            return _entityTypes.TryGetValue(entitySet, out setTypes) && setTypes.Contains(entityType);
        }
        public void Can_get_and_set_configuration_annotation()
        {
            var entityType = new EdmEntityType();

            entityType.SetConfiguration(42);

            Assert.Equal(42, entityType.GetConfiguration());
        }
        public void Configure_should_throw_when_property_not_found()
        {
            var entityType = new EdmEntityType { Name = "E" };
            var entityTypeConfiguration = new EntityTypeConfiguration(typeof(object));
            var mockPropertyConfiguration = new Mock<PrimitivePropertyConfiguration>();
            entityTypeConfiguration.Property(new PropertyPath(new MockPropertyInfo()), () => mockPropertyConfiguration.Object);

            Assert.Equal(Strings.PropertyNotFound(("P"), "E"), Assert.Throws<InvalidOperationException>(() => entityTypeConfiguration.Configure(entityType, new EdmModel())).Message);
        }
        public void Configure_should_set_configuration()
        {
            var entityType = new EdmEntityType { Name = "E" };
            var entityTypeConfiguration = new EntityTypeConfiguration(typeof(object));

            entityTypeConfiguration.Configure(entityType, new EdmModel());

            Assert.Same(entityTypeConfiguration, entityType.GetConfiguration());
        }
        public void Apply_should_ignore_non_primitive_type()
        {
            var entityType = new EdmEntityType();
            var property = entityType.AddPrimitiveProperty("Id");

            ((IEdmConvention<EdmEntityType>)new IdKeyDiscoveryConvention()).Apply(entityType, new EdmModel());

            Assert.False(entityType.DeclaredKeyProperties.Contains(property));
        }
        public void Apply_should_ignore_case()
        {
            var entityType = new EdmEntityType { Name = "Foo" };
            var property = entityType.AddPrimitiveProperty("foOid");
            property.PropertyType.EdmType = EdmPrimitiveType.Int32;

            ((IEdmConvention<EdmEntityType>)new IdKeyDiscoveryConvention()).Apply(entityType, new EdmModel());

            Assert.True(entityType.DeclaredKeyProperties.Contains(property));
        }
        public void Apply_should_make_key_not_nullable()
        {
            var entityType = new EdmEntityType();
            var property = entityType.AddPrimitiveProperty("Id");
            property.PropertyType.EdmType = EdmPrimitiveType.Int32;

            ((IEdmConvention<EdmEntityType>)new IdKeyDiscoveryConvention()).Apply(entityType, new EdmModel());

            Assert.False(property.PropertyType.IsNullable.Value);
        }
        public void Apply_should_match_simple_id()
        {
            var entityType = new EdmEntityType();
            var property = entityType.AddPrimitiveProperty("Id");
            property.PropertyType.EdmType = EdmPrimitiveType.Int32;

            ((IEdmConvention<EdmEntityType>)new IdKeyDiscoveryConvention()).Apply(entityType, new EdmModel());

            Assert.True(entityType.DeclaredKeyProperties.Contains(property));
        }
 public void AddMapping(
     EdmEntityType entityType,
     IList<EdmProperty> propertyPath,
     IEnumerable<DbColumnCondition> conditions,
     bool isDefaultDiscriminatorCondition)
 {
     _propertyMappings.Add(
         new PropertyMappingSpecification(
             entityType, propertyPath, conditions.ToList(), isDefaultDiscriminatorCondition));
 }
        public void GetPrimitiveProperties_should_return_only_primitive_properties()
        {
            var entityType = new EdmEntityType();
            var property = entityType.AddPrimitiveProperty("Foo");
            property.PropertyType.EdmType = EdmPrimitiveType.DateTime;
            entityType.AddComplexProperty("Bar", new EdmComplexType());

            Assert.Equal(1, entityType.GetDeclaredPrimitiveProperties().Count());
            Assert.True(entityType.GetDeclaredPrimitiveProperties().Contains(property));
        }
        public void Can_get_and_set_clr_type_annotation()
        {
            var entityType = new EdmEntityType();

            Assert.Null(entityType.GetClrType());

            entityType.SetClrType(typeof(object));

            Assert.Equal(typeof(object), entityType.GetClrType());
        }
        public void Map_should_map_entity_navigation_properties()
        {
            var entityType = new EdmEntityType();
            var mappingContext
                = new MappingContext(new ModelConfiguration(), new ConventionsConfiguration(), new EdmModel().Initialize());

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

            Assert.Equal(1, entityType.DeclaredNavigationProperties.Count);
        }
        public TestModelBuilder Entity(string name, bool addSet = true)
        {
            _entityType = _model.AddEntityType(name);
            _entityType.SetClrType(new MockType(name));

            if (addSet)
            {
                _model.AddEntitySet(name + "Set", _entityType);
            }

            return this;
        }
        public void AddComplexProperty_should_create_and_add_complex_property()
        {
            var entityType = new EdmEntityType();

            var complexType = new EdmComplexType();
            var property = entityType.AddComplexProperty("Foo", complexType);

            Assert.NotNull(property);
            Assert.Equal("Foo", property.Name);
            Assert.Same(complexType, property.PropertyType.ComplexType);
            Assert.True(entityType.DeclaredProperties.Contains(property));
        }
        public void AddNavigationProperty_should_create_and_add_navigation_property()
        {
            var entityType = new EdmEntityType();
            var associationType = new EdmAssociationType();

            var navigationProperty = entityType.AddNavigationProperty("N", associationType);

            Assert.NotNull(navigationProperty);
            Assert.Equal("N", navigationProperty.Name);
            Assert.Same(associationType, navigationProperty.Association);
            Assert.True(entityType.NavigationProperties.Contains(navigationProperty));
        }
        public void Apply_should_match_id_ahead_of_type_and_id()
        {
            var entityType = new EdmEntityType { Name = "Foo" };
            var typeIdProperty = entityType.AddPrimitiveProperty("FooId");
            var idProperty = entityType.AddPrimitiveProperty("Id");
            typeIdProperty.PropertyType.EdmType = EdmPrimitiveType.Int32;
            idProperty.PropertyType.EdmType = EdmPrimitiveType.Int32;

            ((IEdmConvention<EdmEntityType>)new IdKeyDiscoveryConvention()).Apply(entityType, new EdmModel());

            Assert.Equal(1, entityType.DeclaredKeyProperties.Count);
            Assert.True(entityType.DeclaredKeyProperties.Contains(idProperty));
        }
        public void Map_should_not_detect_arrays_as_collection_associations()
        {
            var modelConfiguration = new ModelConfiguration();
            var model = new EdmModel().Initialize();
            var entityType = new EdmEntityType();
            var mappingContext = new MappingContext(modelConfiguration, new ConventionsConfiguration(), model);

            new NavigationPropertyMapper(new TypeMapper(mappingContext))
                .Map(new MockPropertyInfo(typeof(NavigationPropertyMapperTests[]), "Nav"), entityType,
                    () => new EntityTypeConfiguration(typeof(object)));

            Assert.Equal(0, model.Namespaces.Single().AssociationTypes.Count);
        }
        public void Configure_should_configure_entity_set_name()
        {
            var model = new EdmModel().Initialize();
            var entityType = new EdmEntityType { Name = "E" };
            var entitySet = model.AddEntitySet("ESet", entityType);

            var entityTypeConfiguration = new EntityTypeConfiguration(typeof(object)) { EntitySetName = "MySet" };

            entityTypeConfiguration.Configure(entityType, model);

            Assert.Equal("MySet", entitySet.Name);
            Assert.Same(entityTypeConfiguration, entitySet.GetConfiguration());
        }
        public PropertyMappingSpecification(
            EdmEntityType entityType,
            IList<EdmProperty> propertyPath,
            IList<DbColumnCondition> conditions,
            bool isDefaultDiscriminatorCondition)
        {
            //Contract.Requires(entityType != null);

            _entityType = entityType;
            _propertyPath = propertyPath;
            _conditions = conditions;
            _isDefaultDiscriminatorCondition = isDefaultDiscriminatorCondition;
        }
        public void Generate_should_add_set_mapping_and_table_and_set_clr_type()
        {
            var databaseMapping = CreateEmptyModel();
            var entityType = new EdmEntityType { Name = "E" };
            var entitySet = databaseMapping.Model.AddEntitySet("ESet", entityType);
            entityType.SetClrType(typeof(object));

            new EntityTypeMappingGenerator(ProviderRegistry.Sql2008_ProviderManifest).Generate(entityType, databaseMapping);

            Assert.NotNull(databaseMapping.GetEntitySetMapping(entitySet));
            Assert.Same(entityType, databaseMapping.GetEntitySetMapping(entitySet).EntityTypeMappings.Single().EntityType);
            Assert.Same(typeof(object), databaseMapping.GetEntitySetMapping(entitySet).EntityTypeMappings.Single().GetClrType());
            Assert.NotNull(databaseMapping.GetEntitySetMapping(entitySet).EntityTypeMappings.Single().TypeMappingFragments.Single().Table);
        }
        public void Apply_should_match_simple_short_key()
        {
            var entityType = new EdmEntityType();
            var property = new EdmProperty().AsPrimitive();
            property.PropertyType.EdmType = EdmPrimitiveType.Int16;
            entityType.DeclaredKeyProperties.Add(property);

            ((IEdmConvention<EdmEntityType>)new StoreGeneratedIdentityKeyConvention())
                .Apply(entityType, new EdmModel().Initialize());

            Assert.Equal(
                DbStoreGeneratedPattern.Identity,
                entityType.DeclaredKeyProperties.Single().GetStoreGeneratedPattern());
        }
        public void Apply_should_set_correct_defaults_for_unconfigured_strings()
        {
            var entityType = new EdmEntityType();
            var property = new EdmProperty().AsPrimitive();
            property.PropertyType.EdmType = EdmPrimitiveType.String;
            entityType.DeclaredProperties.Add(property);

            ((IEdmConvention<EdmEntityType>)new SqlCePropertyMaxLengthConvention())
                .Apply(entityType, CreateEdmModel());

            var primitiveTypeFacets = property.PropertyType.PrimitiveTypeFacets;

            Assert.Equal(4000, primitiveTypeFacets.MaxLength);
        }
        public void Configure_should_configure_properties()
        {
            var entityType = new EdmEntityType { Name = "E" };
            var property = entityType.AddPrimitiveProperty("P");
            property.PropertyType.EdmType = EdmPrimitiveType.Int32;
            var entityTypeConfiguration = new EntityTypeConfiguration(typeof(object));
            var mockPropertyConfiguration = new Mock<PrimitivePropertyConfiguration>();
            var mockPropertyInfo = new MockPropertyInfo();
            property.SetClrPropertyInfo(mockPropertyInfo);
            entityTypeConfiguration.Property(new PropertyPath(mockPropertyInfo), () => mockPropertyConfiguration.Object);

            entityTypeConfiguration.Configure(entityType, new EdmModel());

            mockPropertyConfiguration.Verify(p => p.Configure(property));
        }
        public static bool IsAncestorOf(this EdmEntityType ancestor, EdmEntityType entityType)
        {
            //Contract.Requires(ancestor != null);
            //Contract.Requires(entityType != null);

            while (entityType != null)
            {
                if (entityType.BaseType == ancestor)
                {
                    return true;
                }
                entityType = entityType.BaseType;
            }
            return false;
        }
        public void Map_should_set_correct_name_and_type()
        {
            var entityType = new EdmEntityType();
            var mappingContext
                = new MappingContext(new ModelConfiguration(), new ConventionsConfiguration(), new EdmModel().Initialize());

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

            Assert.Equal(1, entityType.DeclaredProperties.Count);

            var property = entityType.GetDeclaredPrimitiveProperty("Foo");

            Assert.Equal(EdmPrimitiveType.Int32, property.PropertyType.PrimitiveType);
        }
        public void Apply_should_set_correct_defaults_for_unconfigured_binary()
        {
            var entityType = new EdmEntityType();
            var property = new EdmProperty().AsPrimitive();
            property.PropertyType.EdmType = EdmPrimitiveType.Binary;
            entityType.DeclaredProperties.Add(property);

            ((IEdmConvention<EdmEntityType>)new PropertyMaxLengthConvention())
                .Apply(entityType, new EdmModel());

            var primitiveTypeFacets = property.PropertyType.PrimitiveTypeFacets;

            Assert.Null(primitiveTypeFacets.IsUnicode);
            Assert.Equal(false, primitiveTypeFacets.IsFixedLength);
            Assert.Null(primitiveTypeFacets.MaxLength);
        }