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 Generate_should_map_scalar_properties_to_columns()
        {
            var databaseMapping = CreateEmptyModel();
            var entityType = new EdmEntityType { Name = "E" };
            entityType.AddPrimitiveProperty("P1").PropertyType.EdmType = EdmPrimitiveType.Int32;
            entityType.AddPrimitiveProperty("P2").PropertyType.EdmType = EdmPrimitiveType.String;
            var entitySet = databaseMapping.Model.AddEntitySet("ESet", entityType);
            entityType.SetClrType(typeof(object));

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

            var entityTypeMappingFragment
                = databaseMapping.GetEntitySetMapping(entitySet).EntityTypeMappings.Single().TypeMappingFragments.Single();

            Assert.Equal(2, entityTypeMappingFragment.PropertyMappings.Count());
            Assert.Equal(2, entityTypeMappingFragment.Table.Columns.Count());
        }
        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 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_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 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 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 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 void Configure_should_add_properties_to_dependent_properties()
        {
            var mockPropertyInfo = new MockPropertyInfo(typeof(int), "P");
            var constraintConfiguration = new ForeignKeyConstraintConfiguration(new[] { mockPropertyInfo.Object });
            var entityType = new EdmEntityType();
            var property = entityType.AddPrimitiveProperty("P");
            property.PropertyType.EdmType = EdmPrimitiveType.Int32;
            property.SetClrPropertyInfo(mockPropertyInfo);
            var associationType = new EdmAssociationType().Initialize();
            associationType.SourceEnd.EntityType = entityType;
            
            constraintConfiguration.Configure(
                associationType, associationType.SourceEnd, new EntityTypeConfiguration(typeof(object)));

            Assert.Equal(1, associationType.Constraint.DependentProperties.Count);
        }
        public void Configure_should_propagate_end_kind_to_keys_when_required()
        {
            var mockPropertyInfo = new MockPropertyInfo(typeof(int), "P");
            var constraintConfiguration = new ForeignKeyConstraintConfiguration(new[] { mockPropertyInfo.Object });
            var entityType = new EdmEntityType();
            var property = entityType.AddPrimitiveProperty("P");
            property.PropertyType.EdmType = EdmPrimitiveType.Int32;
            property.PropertyType.IsNullable = true;
            property.SetClrPropertyInfo(mockPropertyInfo);
            var associationType = new EdmAssociationType().Initialize();
            associationType.SourceEnd.EntityType = entityType;
            associationType.TargetEnd.EndKind = EdmAssociationEndKind.Required;

            constraintConfiguration.Configure(
                associationType, associationType.SourceEnd, new EntityTypeConfiguration(typeof(object)));

            Assert.Equal(1, associationType.Constraint.DependentProperties.Count);
            Assert.Equal(false, property.PropertyType.IsNullable);
        }
        private static EdmModel CreateModelFixture(
            out EdmEntityType declaringEntityType, out EdmEntityType complexEntityType)
        {
            var model = new EdmModel().Initialize();

            declaringEntityType = model.AddEntityType("E");
            complexEntityType = model.AddEntityType("C");
            complexEntityType.AddPrimitiveProperty("P");

            var associationType
                = model.AddAssociationType(
                    "A",
                    declaringEntityType, EdmAssociationEndKind.Many,
                    complexEntityType, EdmAssociationEndKind.Optional);

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

            return model;
        }
        public void Apply_should_ignore_when_key_already_specified()
        {
            var entityType = new EdmEntityType();
            var property = entityType.AddPrimitiveProperty("Id");
            entityType.DeclaredKeyProperties.Add(new EdmProperty());

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

            Assert.False(entityType.DeclaredKeyProperties.Contains(property));
        }
        [Fact] // Dev11 347225
        public void Apply_should_throw_if_two_type_Id_properties_are_matched_that_differ_only_by_case()
        {
            var entityType = new EdmEntityType { Name = "Foo" };
            var FOOIdProperty = entityType.AddPrimitiveProperty("FOOId");
            var FooIdProperty = entityType.AddPrimitiveProperty("FooId");
            FOOIdProperty.PropertyType.EdmType = EdmPrimitiveType.Int32;
            FooIdProperty.PropertyType.EdmType = EdmPrimitiveType.Int32;

            Assert.Equal(Strings.MultiplePropertiesMatchedAsKeys("FOOId", "Foo"), Assert.Throws<InvalidOperationException>(() => ((IEdmConvention<EdmEntityType>)new IdKeyDiscoveryConvention()).Apply(entityType, new EdmModel())).Message);
        }
        public void GetPrimitiveProperty_should_return_correct_property()
        {
            var entityType = new EdmEntityType();
            var property = entityType.AddPrimitiveProperty("Foo");
            property.PropertyType.EdmType = EdmPrimitiveType.Guid;

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

            Assert.NotNull(foundProperty);
            Assert.Same(property, foundProperty);
        }
        public void Configure_should_configure_and_order_keys_when_keys_and_order_specified()
        {
            var entityType = new EdmEntityType { Name = "E" };
            entityType.AddPrimitiveProperty("P2").PropertyType.EdmType = EdmPrimitiveType.Int32;
            entityType.AddPrimitiveProperty("P1").PropertyType.EdmType = EdmPrimitiveType.Int32;

            var entityTypeConfiguration = new EntityTypeConfiguration(typeof(object));
            var mockPropertyInfo2 = new MockPropertyInfo(typeof(int), "P2");
            entityTypeConfiguration.Key(mockPropertyInfo2);
            entityTypeConfiguration.Property(new PropertyPath(mockPropertyInfo2)).ColumnOrder = 1;
            entityType.GetDeclaredPrimitiveProperty("P2").SetClrPropertyInfo(mockPropertyInfo2);
            var mockPropertyInfo1 = new MockPropertyInfo(typeof(int), "P1");
            entityTypeConfiguration.Key(mockPropertyInfo1);
            entityTypeConfiguration.Property(new PropertyPath(mockPropertyInfo1)).ColumnOrder = 0;
            entityType.GetDeclaredPrimitiveProperty("P1").SetClrPropertyInfo(mockPropertyInfo1);

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

            Assert.Equal(2, entityType.DeclaredKeyProperties.Count);
            Assert.Equal("P1", entityType.DeclaredKeyProperties.First().Name);
        }
        public void AddPrimitiveProperty_should_create_and_add_to_declared_properties()
        {
            var entityType = new EdmEntityType();

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

            Assert.NotNull(property);
            Assert.Equal("Foo", property.Name);
            Assert.True(entityType.DeclaredProperties.Contains(property));
        }