public void Generate_should_flatten_complex_properties_to_columns()
        {
            var databaseMapping = CreateEmptyModel();
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var complexType = new ComplexType("C");

            var property = EdmProperty.CreatePrimitive("P1", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            complexType.AddMember(property);
            entityType.AddComplexProperty("C1", complexType);
            var property1 = EdmProperty.CreatePrimitive("P2", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property1);
            var entitySet = databaseMapping.Model.AddEntitySet("ESet", entityType);
            var type = typeof(object);

            entityType.GetMetadataProperties().SetClrType(type);

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

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

            Assert.Equal(2, entityTypeMappingFragment.ColumnMappings.Count());
            Assert.Equal(2, entityTypeMappingFragment.Table.Properties.Count());
        }
        public void Can_get_and_set_configuration_annotation()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);

            entityType.GetMetadataProperties().SetConfiguration(42);

            Assert.Equal(42, entityType.GetConfiguration());
        }
        public void GetClrType_returns_CLR_type_annotation_for_EntityType()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);

            Assert.Null(((EdmType)entityType).GetClrType());

            entityType.GetMetadataProperties().SetClrType(typeof(Random));

            Assert.Same(typeof(Random), ((EdmType)entityType).GetClrType());
        }
        public void Can_get_and_set_clr_type_annotation()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);

            Assert.Null(entityType.GetClrType());

            var type = typeof(object);

            entityType.GetMetadataProperties().SetClrType(type);

            Assert.Equal(typeof(object), entityType.GetClrType());
        }
        public TestModelBuilder Entity(string name, bool addSet = true)
        {
            _entityType = _model.AddEntityType(name);

            Type type = new MockType(name);

            _entityType.GetMetadataProperties().SetClrType(type);

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

            return this;
        }
        public void Apply_should_move_declared_keys_head_of_declared_properties_list()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var type = typeof(SimpleEntity);

            entityType.GetMetadataProperties().SetClrType(type);

            var property1 = EdmProperty.CreatePrimitive("PrivateProperty", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property1);

            var property2 = EdmProperty.CreatePrimitive("InheritedPropertyB", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property2);

            var property3 = EdmProperty.CreatePrimitive("InheritedPropertyA", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property3);

            var property4 = EdmProperty.CreatePrimitive("PropertyB", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property4);

            var property5 = EdmProperty.CreatePrimitive("PropertyA", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property5);

            var property6 = EdmProperty.CreatePrimitive("Key", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property6);

            entityType.AddKeyMember(property6);

            new DeclaredPropertyOrderingConvention().Apply(entityType, new DbModel(new EdmModel(DataSpace.CSpace), null));

            Assert.True(
                entityType.DeclaredProperties.Select(e => e.Name)
                    .SequenceEqual(
                        new[]
                            {
                                "Key",
                                "PrivateProperty",
                                "PropertyA",
                                "PropertyB",
                                "InheritedPropertyA",
                                "InheritedPropertyB"
                            }));
        }
        public void Generate_should_add_set_mapping_and_table_and_set_clr_type()
        {
            var databaseMapping = CreateEmptyModel();
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var entitySet = databaseMapping.Model.AddEntitySet("ESet", entityType);
            var type = typeof(object);

            entityType.GetMetadataProperties().SetClrType(type);

            new TableMappingGenerator(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().MappingFragments.Single().Table);
        }
        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 EntityType("SE", "N", DataSpace.CSpace);
            entityType.GetMetadataProperties().SetClrType(typeof(object));
            var property1 = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property1);
            var property = property1;
            property.SetClrPropertyInfo(mockPropertyInfo);
            var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace);
            associationType.SourceEnd = new AssociationEndMember("S", entityType);
            associationType.TargetEnd = new AssociationEndMember("T", new EntityType("TE", "N", DataSpace.CSpace));

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

            Assert.Equal(1, associationType.Constraint.ToProperties.Count);
        }
        public void Apply_should_match_key_that_is_an_fk_used_in_table_splitting()
        {
            var model = new EdmModel(DataSpace.CSpace);
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var property = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int64));

            entityType.AddKeyMember(property);

            var targetConfig = new EntityTypeConfiguration(typeof(object));
            targetConfig.ToTable("SharedTable");
            entityType.GetMetadataProperties().SetConfiguration(targetConfig);

            var sourceEntityType = new EntityType("E", "N", DataSpace.CSpace);
            var sourceConfig = new EntityTypeConfiguration(typeof(object));
            sourceConfig.ToTable("SharedTable");
            sourceEntityType.GetMetadataProperties().SetConfiguration(sourceConfig);

            var associationType
                = model.AddAssociationType(
                    "A", sourceEntityType, RelationshipMultiplicity.One,
                    entityType, RelationshipMultiplicity.One);

            associationType.Constraint
                = new ReferentialConstraint(
                    associationType.SourceEnd,
                    associationType.TargetEnd,
                    new[] { property },
                    new[] { property });

            (new StoreGeneratedIdentityKeyConvention())
                .Apply(entityType, new DbModel(model, null));

            Assert.Equal(
                StoreGeneratedPattern.Identity,
                entityType.KeyProperties.Single().GetStoreGeneratedPattern());
        }
        public void Configure_should_throw_when_dependent_property_not_found()
        {
            var constraintConfiguration
                = new ForeignKeyConstraintConfiguration(
                    new[]
                        {
                            new MockPropertyInfo(typeof(int), "P").Object
                        });
            var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace);

            var entityType = new EntityType("T", "N", DataSpace.CSpace);
            entityType.GetMetadataProperties().SetClrType(typeof(object));

            Assert.Equal(
                Strings.ForeignKeyPropertyNotFound("P", "T"),
                Assert.Throws<InvalidOperationException>(
                    () => constraintConfiguration.Configure(
                        associationType,
                        new AssociationEndMember("E", entityType)
                              , new EntityTypeConfiguration(typeof(object)))).Message);
        }
        internal virtual void Configure(EntityType entityType, EdmModel model)
        {
            DebugCheck.NotNull(entityType);
            DebugCheck.NotNull(model);

            ConfigureKey(entityType);
            Configure(entityType.Name, entityType.Properties, entityType.GetMetadataProperties());
            ConfigureAssociations(entityType, model);
            ConfigureEntitySetName(entityType, model);
        }
        public void GetEntityTypeMapping_should_return_mapping_for_type_by_clrType()
        {
            var databaseMapping = new DbDatabaseMapping()
                .Initialize(new EdmModel(DataSpace.CSpace), new EdmModel(DataSpace.SSpace));
            var entityType = new EntityType("Foo", "N", DataSpace.CSpace);
            var type = typeof(object);

            entityType.GetMetadataProperties().SetClrType(type);
            var entityTypeMapping = new EntityTypeMapping(null);
            entityTypeMapping.AddType(entityType);
            entityTypeMapping.SetClrType(typeof(object));
            databaseMapping.AddEntitySetMapping(
                new EntitySet
                    {
                        Name = "ES"
                    }).AddTypeMapping(entityTypeMapping);

            Assert.Same(entityTypeMapping, databaseMapping.GetEntityTypeMapping(typeof(object)));
        }