public void Can_create_with_valid_complex_type()
        {
            var complexType = new ComplexType();
            var complexTypeMapping = new ComplexTypeMapping(complexType);

            Assert.Same(complexType, complexTypeMapping.ComplexType);
        }
        public void Can_generate_scalar_and_complex_properties_when_update()
        {
            var functionParameterMappingGenerator
                = new FunctionParameterMappingGenerator(ProviderRegistry.Sql2008_ProviderManifest);

            var property0 = new EdmProperty("P0");
            var property1 = new EdmProperty("P1");
            var property2 = new EdmProperty("P2");

            property2.SetStoreGeneratedPattern(StoreGeneratedPattern.Computed);
            property2.ConcurrencyMode = ConcurrencyMode.Fixed;

            var complexType = new ComplexType("CT", "N", DataSpace.CSpace);

            complexType.AddMember(property1);

            var complexProperty = EdmProperty.CreateComplex("C", complexType);

            var parameterBindings
                = functionParameterMappingGenerator
                    .Generate(
                        ModificationOperator.Update,
                        new[] { property0, complexProperty, property2 },
                        new[]
                            {
                                new ColumnMappingBuilder(new EdmProperty("C0"), new[] { property0 }),
                                new ColumnMappingBuilder(new EdmProperty("C_P1"), new[] { complexProperty, property1 }),
                                new ColumnMappingBuilder(new EdmProperty("C2"), new[] { property2 })
                            },
                        new List<EdmProperty>(),
                        useOriginalValues: true)
                    .ToList();

            Assert.Equal(3, parameterBindings.Count());

            var parameterBinding = parameterBindings.First();

            Assert.Equal("C0", parameterBinding.Parameter.Name);
            Assert.Same(property0, parameterBinding.MemberPath.Members.Single());
            Assert.Equal("String", parameterBinding.Parameter.TypeName);
            Assert.Equal(ParameterMode.In, parameterBinding.Parameter.Mode);
            Assert.False(parameterBinding.IsCurrent);

            parameterBinding = parameterBindings.ElementAt(1);

            Assert.Equal("C_P1", parameterBinding.Parameter.Name);
            Assert.Same(complexProperty, parameterBinding.MemberPath.Members.First());
            Assert.Same(property1, parameterBinding.MemberPath.Members.Last());
            Assert.Equal("String", parameterBinding.Parameter.TypeName);
            Assert.Equal(ParameterMode.In, parameterBinding.Parameter.Mode);
            Assert.False(parameterBinding.IsCurrent);

            parameterBinding = parameterBindings.Last();

            Assert.Equal("C2_Original", parameterBinding.Parameter.Name);
            Assert.Same(property2, parameterBinding.MemberPath.Members.Single());
            Assert.Equal("String", parameterBinding.Parameter.TypeName);
            Assert.Equal(ParameterMode.In, parameterBinding.Parameter.Mode);
            Assert.False(parameterBinding.IsCurrent);
        }
        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.Primitive("P1", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

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

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

            entityType.Annotations.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());
        }
        internal FunctionImportComplexTypeMapping(
            ComplexType returnType, Collection<FunctionImportReturnTypePropertyMapping> properties, LineInfo lineInfo)
            : base(properties, lineInfo)
        {
            DebugCheck.NotNull(returnType);
            DebugCheck.NotNull(properties);

            _returnType = returnType;
        }
        public void Configure_should_set_configuration()
        {
            var complexType = new ComplexType("C");
            var complexTypeConfiguration = new ComplexTypeConfiguration(typeof(object));

            complexTypeConfiguration.Configure(complexType);

            Assert.Same(complexTypeConfiguration, complexType.GetConfiguration());
        }
 /// <summary>
 /// Initializes a new FunctionImportComplexTypeMapping instance.
 /// </summary>
 /// <param name="returnType">The return type.</param>
 /// <param name="properties">The property mappings for the result type of a function import.</param>
 public FunctionImportComplexTypeMapping(
     ComplexType returnType, 
     Collection<FunctionImportReturnTypePropertyMapping> properties)
     : this(
         Check.NotNull(returnType, "returnType"),
         Check.NotNull(properties, "properties"), 
         LineInfo.Empty)
 {
 }
        public void GetClrType_returns_CLR_type_annotation_for_ComplexType()
        {
            var complexType = new ComplexType("C", "N", DataSpace.CSpace);

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

            complexType.Annotations.SetClrType(typeof(Random));

            Assert.Same(typeof(Random), ((EdmType)complexType).GetClrType());
        }
        public void Complex_should_create_complex_property()
        {
            var complexType = new ComplexType();

            var property = EdmProperty.CreateComplex("P", complexType);

            Assert.NotNull(property);
            Assert.NotNull(property.TypeUsage);
            Assert.Same(complexType, property.TypeUsage.EdmType);
        }
        public void IsComplexType_returns_true_when_complex_property()
        {
            var complexType = new ComplexType();

            var property = EdmProperty.CreateComplex("P", complexType);

            Assert.False(property.IsPrimitiveType);
            Assert.False(property.IsEnumType);
            Assert.True(property.IsComplexType);
        }
        public void Configure_should_throw_when_property_not_found()
        {
            var complexType = new ComplexType("C");
            var complexTypeConfiguration = new ComplexTypeConfiguration(typeof(object));
            var mockPropertyConfiguration = new Mock<PrimitivePropertyConfiguration>();
            complexTypeConfiguration.Property(new PropertyPath(new MockPropertyInfo()), () => mockPropertyConfiguration.Object);

            Assert.Equal(
                Strings.PropertyNotFound(("P"), "C"),
                Assert.Throws<InvalidOperationException>(() => complexTypeConfiguration.Configure(complexType)).Message);
        }
        public void AddComplexProperty_should_create_and_add_complex_property()
        {
            var complexType = new ComplexType("C");
            var complexTypeProperty = new ComplexType("D");
            var property = complexType.AddComplexProperty("Foo", complexTypeProperty);

            Assert.NotNull(property);
            Assert.Equal("Foo", property.Name);
            Assert.Same(complexTypeProperty, property.ComplexType);
            Assert.True(complexType.Properties.Contains(property));
        }
        public static EdmProperty Complex(string name, ComplexType complexType)
        {
            Check.NotEmpty(name, "name");
            Check.NotNull(complexType, "complexType");

            var property = CreateProperty(name, complexType);

            property.Nullable = false;

            return property;
        }
        public void AddPrimitiveProperty_should_create_and_add_to_primitive_properties()
        {
            var complexType = new ComplexType("C");
            var property1 = EdmProperty.CreatePrimitive("Foo", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            complexType.AddMember(property1);
            var property = property1;

            Assert.NotNull(property);
            Assert.Equal("Foo", property.Name);
            Assert.True(complexType.Properties.Contains(property));
        }
        public void Properties_list_should_be_live_on_reread()
        {
            var complexType = new ComplexType("C");

            Assert.Empty(complexType.Properties);

            var property = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            complexType.AddMember(property);

            Assert.Equal(1, complexType.Properties.Count);
        }
        public void Should_be_able_to_get_and_set_clr_type()
        {
            var complexType = new ComplexType("C");

            Assert.Null(complexType.GetClrType());

            var type = typeof(object);

            complexType.Annotations.SetClrType(type);

            Assert.Equal(typeof(object), complexType.GetClrType());
        }
        public void AddComplexProperty_should_create_and_add_complex_property()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);

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

            Assert.NotNull(property);
            Assert.Equal("Foo", property.Name);
            Assert.Same(complexType, property.ComplexType);
            Assert.True(entityType.DeclaredProperties.Contains(property));
        }
        public void Should_be_able_to_get_and_set_clr_type()
        {
            var complexType = new ComplexType("C");

            Assert.Null(complexType.GetClrType());

            var type = typeof(object);

            complexType.GetMetadataProperties().SetClrType(type);

            Assert.Equal(typeof(object), complexType.GetClrType());
        }
        public void GetPrimitiveProperty_should_return_correct_property()
        {
            var complexType = new ComplexType("C");
            var property1 = EdmProperty.CreatePrimitive("Foo", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            complexType.AddMember(property1);
            var property = property1;

            var foundProperty = complexType.Properties.SingleOrDefault(p => p.Name == "Foo");

            Assert.NotNull(foundProperty);
            Assert.Same(property, foundProperty);
        }
Beispiel #19
0
        public void GetComplexParameterBindings_should_return_all_complex_parameter_bindings_for_type()
        {
            var databaseMapping
                = new DbDatabaseMapping()
                    .Initialize(
                        new EdmModel(DataSpace.CSpace),
                        new EdmModel(DataSpace.SSpace));

            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var entitySet = databaseMapping.Model.AddEntitySet("ES", entityType);
            var entitySetMapping = databaseMapping.AddEntitySetMapping(entitySet);

            var complexType1 = new ComplexType();
            complexType1.Annotations.SetClrType(typeof(string));

            var complexType2 = new ComplexType();
            complexType2.Annotations.SetClrType(typeof(object));

            var storageModificationFunctionMapping
                = new StorageModificationFunctionMapping(
                    entitySet,
                    entityType,
                    new EdmFunction("F", "N", DataSpace.SSpace),
                    new[]
                        {
                            new StorageModificationFunctionParameterBinding(
                                new FunctionParameter(),
                                new StorageModificationFunctionMemberPath(
                                new EdmMember[]
                                    {
                                        EdmProperty.Complex("C1", complexType1),
                                        EdmProperty.Complex("C2", complexType2),
                                        new EdmProperty("M")
                                    },
                                null),
                                true
                                )
                        },
                    null,
                    null);

            entitySetMapping.AddModificationFunctionMapping(
                new StorageEntityTypeModificationFunctionMapping(
                    entityType,
                    storageModificationFunctionMapping,
                    storageModificationFunctionMapping,
                    storageModificationFunctionMapping));

            Assert.Equal(3, databaseMapping.GetComplexParameterBindings(typeof(string)).Count());
            Assert.Equal(3, databaseMapping.GetComplexParameterBindings(typeof(object)).Count());
        }
        public static EdmProperty AddComplexProperty(
            this ComplexType complexType, string name, ComplexType targetComplexType)
        {
            DebugCheck.NotNull(complexType);
            DebugCheck.NotNull(complexType.Properties);
            DebugCheck.NotEmpty(name);
            DebugCheck.NotNull(targetComplexType);

            var property = EdmProperty.CreateComplex(name, targetComplexType);

            complexType.AddMember(property);

            return property;
        }
        public void Cannot_add_property_when_read_only()
        {
            var complexType = new ComplexType();
            var complexTypeMapping = new ComplexTypeMapping(complexType);

            complexTypeMapping.SetReadOnly();

            var scalarPropertyMapping = new ScalarPropertyMapping(new EdmProperty("P"), new EdmProperty("C", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })));

            Assert.Equal(
                Strings.OperationOnReadOnlyItem,
                Assert.Throws<InvalidOperationException>(
                    () => complexTypeMapping.AddPropertyMapping(scalarPropertyMapping)).Message);
        }
        public void Map_should_map_complex_type_properties()
        {
            var complexType = new ComplexType("C");
            var mappingContext
                = new MappingContext(new ModelConfiguration(), new ConventionsConfiguration(), new EdmModel(DataSpace.CSpace));

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

            Assert.Equal(1, complexType.Properties.Count);

            var property = complexType.Properties.SingleOrDefault(p => p.Name == "Foo");

            Assert.Equal(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32), property.PrimitiveType);
        }
        public void Map(
            PropertyInfo propertyInfo, ComplexType complexType,
            Func<ComplexTypeConfiguration> complexTypeConfiguration)
        {
            DebugCheck.NotNull(propertyInfo);
            DebugCheck.NotNull(complexType);

            var property = MapPrimitiveOrComplexOrEnumProperty(
                propertyInfo, complexTypeConfiguration, discoverComplexTypes: true);

            if (property != null)
            {
                complexType.AddMember(property);
            }
        }
        public void Can_add_remove_properties()
        {
            var complexType = new ComplexType();
            var complexTypeMapping = new ComplexTypeMapping(complexType);
            var scalarPropertyMapping = new ScalarPropertyMapping(new EdmProperty("P"), new EdmProperty("C", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })));

            Assert.Empty(complexTypeMapping.PropertyMappings);

            complexTypeMapping.AddPropertyMapping(scalarPropertyMapping);

            Assert.Same(scalarPropertyMapping, complexTypeMapping.PropertyMappings.Single());

            complexTypeMapping.RemovePropertyMapping(scalarPropertyMapping);

            Assert.Empty(complexTypeMapping.PropertyMappings);
        }
        public void Can_add_remove_conditions()
        {
            var complexType = new ComplexType();
            var complexTypeMapping = new ComplexTypeMapping(complexType);
            var conditionMapping = new IsNullConditionMapping(new EdmProperty("P"), true);

            Assert.Empty(complexTypeMapping.Conditions);

            complexTypeMapping.AddCondition(conditionMapping);

            Assert.Same(conditionMapping, complexTypeMapping.Conditions.Single());

            complexTypeMapping.RemoveCondition(conditionMapping);

            Assert.Empty(complexTypeMapping.Conditions);
        }
        public void Configure_should_configure_properties()
        {
            var complexType = new ComplexType("C");

            var property1 = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            complexType.AddMember(property1);
            var property = property1;
            var complexTypeConfiguration = new ComplexTypeConfiguration(typeof(object));
            var mockPropertyConfiguration = new Mock<PrimitivePropertyConfiguration>();
            var mockPropertyInfo = new MockPropertyInfo();
            complexTypeConfiguration.Property(new PropertyPath(mockPropertyInfo), () => mockPropertyConfiguration.Object);
            property.SetClrPropertyInfo(mockPropertyInfo);

            complexTypeConfiguration.Configure(complexType);

            mockPropertyConfiguration.Verify(p => p.Configure(property));
        }
Beispiel #27
0
        public void GetComplexPropertyMappings_should_return_all_complex_property_mappings_for_type()
        {
            var databaseMapping = new DbDatabaseMapping()
                .Initialize(new EdmModel(DataSpace.CSpace), new EdmModel(DataSpace.SSpace));
            var entitySet = new EntitySet
                                {
                                    Name = "ES"
                                };
            var entitySetMapping = databaseMapping.AddEntitySetMapping(entitySet);
            var entityTypeMapping = new StorageEntityTypeMapping(null);
            entitySetMapping.AddTypeMapping(entityTypeMapping);
            var entityTypeMappingFragment = new StorageMappingFragment(entitySet, entityTypeMapping, false);
            entityTypeMapping.AddFragment(entityTypeMappingFragment);
            var complexType = new ComplexType("C");
            var propertyMapping1
                = new ColumnMappingBuilder(
                    new EdmProperty("C"),
                    new[]
                        {
                            EdmProperty.Complex("P1", complexType),
                            EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String))
                        });
            var type = typeof(object);

            complexType.Annotations.SetClrType(type);

            entityTypeMappingFragment.AddColumnMapping(propertyMapping1);

            var propertyMapping2
                = new ColumnMappingBuilder(
                    new EdmProperty("C"),
                    new List<EdmProperty>
                        {
                            EdmProperty.Complex("P3", complexType),
                            EdmProperty.Primitive(
                                "P2", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)),
                        });
            entityTypeMappingFragment.AddColumnMapping(propertyMapping2);

            Assert.Equal(2, databaseMapping.GetComplexPropertyMappings(typeof(object)).Count());
        }
Beispiel #28
0
        public void WriteEntityContainerMappingElement_should_write_function_import_elements()
        {
            var typeUsage =
                TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32));

            var complexTypeProperty1 = new EdmProperty("CTProperty1", typeUsage);
            var complexTypeProperty2 = new EdmProperty("CTProperty2", typeUsage);

            var complexType = new ComplexType("CT", "Ns", DataSpace.CSpace);

            complexType.AddMember(complexTypeProperty1);
            complexType.AddMember(complexTypeProperty2);

            var functionImport =
                new EdmFunction(
                    "f_c", "Ns", DataSpace.CSpace,
                    new EdmFunctionPayload
            {
                IsComposable     = true,
                IsFunctionImport = true,
                ReturnParameters =
                    new[]
                {
                    new FunctionParameter(
                        "ReturnValue",
                        TypeUsage.CreateDefaultTypeUsage(complexType.GetCollectionType()),
                        ParameterMode.ReturnValue)
                },
                Parameters =
                    new[]
                {
                    new FunctionParameter("param", typeUsage, ParameterMode.Out)
                }
            });

            typeUsage = ProviderRegistry.Sql2008_ProviderManifest.GetStoreType(typeUsage);
            var rowTypeProperty1 = new EdmProperty("RTProperty1", typeUsage);
            var rowTypeProperty2 = new EdmProperty("RTProperty2", typeUsage);
            var rowType          = new RowType(new[] { rowTypeProperty1, rowTypeProperty2 });

            var storeFunction =
                new EdmFunction(
                    "f_s", "Ns.Store", DataSpace.SSpace,
                    new EdmFunctionPayload
            {
                ReturnParameters =
                    new[]
                {
                    new FunctionParameter(
                        "Return",
                        TypeUsage.CreateDefaultTypeUsage(rowType),
                        ParameterMode.ReturnValue)
                },
                Parameters =
                    new[]
                {
                    new FunctionParameter("param", typeUsage, ParameterMode.Out)
                }
            });

            var structuralTypeMapping =
                new Tuple <StructuralType, List <ConditionPropertyMapping>, List <PropertyMapping> >(
                    complexType, new List <ConditionPropertyMapping>(), new List <PropertyMapping>());

            structuralTypeMapping.Item3.Add(new ScalarPropertyMapping(complexTypeProperty1, rowTypeProperty1));
            structuralTypeMapping.Item3.Add(new ScalarPropertyMapping(complexTypeProperty2, rowTypeProperty2));

            var functionImportMapping = new FunctionImportMappingComposable(
                functionImport,
                storeFunction,
                new List <Tuple <StructuralType, List <ConditionPropertyMapping>, List <PropertyMapping> > >
            {
                structuralTypeMapping
            });

            var containerMapping = new EntityContainerMapping(new EntityContainer("C", DataSpace.SSpace));

            containerMapping.AddFunctionImportMapping(functionImportMapping);

            var fixture = new Fixture();

            fixture.Writer.WriteEntityContainerMappingElement(containerMapping);

            Assert.Equal(
                @"<EntityContainerMapping StorageEntityContainer="""" CdmEntityContainer=""C"">
  <FunctionImportMapping FunctionName=""Ns.Store.f_s"" FunctionImportName=""f_c"">
    <ResultMapping>
      <ComplexTypeMapping TypeName=""Ns.CT"">
        <ScalarProperty Name=""CTProperty1"" ColumnName=""RTProperty1"" />
        <ScalarProperty Name=""CTProperty2"" ColumnName=""RTProperty2"" />
      </ComplexTypeMapping>
    </ResultMapping>
  </FunctionImportMapping>
</EntityContainerMapping>",
                fixture.ToString());
        }
Beispiel #29
0
        public void Can_add_remove_complex_type()
        {
            var model = new EdmModel(DataSpace.SSpace);
            var complexType = new ComplexType("C", "N", DataSpace.SSpace);

            model.AddItem(complexType);

            Assert.True(model.ComplexTypes.Contains(complexType));
            Assert.True(model.NamespaceItems.Contains(complexType));

            model.RemoveItem(complexType);

            Assert.False(model.ComplexTypes.Contains(complexType));
            Assert.False(model.NamespaceItems.Contains(complexType));
        }
 protected virtual void Visit(ComplexType complexType)
 {
     Visit(complexType.BaseType);
     foreach (var member in complexType.Members)
     {
         Visit(member);
     }
     foreach (var property in complexType.Properties)
     {
         Visit(property);
     }
 }
 /// <summary>
 /// Initialize all the build in type with the given type attributes and properties
 /// </summary>
 /// <param name="builtInType">The built In type which is getting initialized</param>
 /// <param name="name">name of the built in type</param>
 /// <param name="isAbstract">whether the type is abstract or not</param>
 /// <param name="isSealed">whether the type is sealed or not</param>
 /// <param name="baseType">The base type of the built in type</param>
 private static void InitializeBuiltInTypes(
     ComplexType builtInType,
     string name,
     bool isAbstract,
     ComplexType baseType)
 {
     // Initialize item attributes for all ancestor types
     EdmType.Initialize(builtInType, name, EdmConstants.EdmNamespace, DataSpace.CSpace, isAbstract, baseType);
 }
        /// <summary>
        /// Removes a complex type from the model.
        /// </summary>
        /// <param name="item">The ComplexType instance to be removed.</param>
        public void RemoveItem(ComplexType item)
        {
            Check.NotNull(item, "item");

            _complexTypes.Remove(item);
        }
Beispiel #33
0
 protected override void VisitComplexType(ComplexType item)
 {
     _schemaWriter.WriteComplexTypeElementHeader(item);
     base.VisitComplexType(item);
     _schemaWriter.WriteEndElement();
 }
 /// <summary>Adds a complex type to the model.</summary>
 /// <param name="item">The ComplexType instance to be added.</param>
 public void AddItem(ComplexType item)
 {
     Check.NotNull <ComplexType>(item, nameof(item));
     this.ValidateSpace((EdmType)item);
     this._complexTypes.Add(item);
 }
 /// <summary>Removes a complex type from the model.</summary>
 /// <param name="item">The ComplexType instance to be removed.</param>
 public void RemoveItem(ComplexType item)
 {
     Check.NotNull <ComplexType>(item, nameof(item));
     this._complexTypes.Remove(item);
 }
Beispiel #36
0
 // <summary>
 // Validate an ComplexType object
 // </summary>
 // <param name="item"> The ComplexType object to validate </param>
 // <param name="errors"> An error collection for adding validation errors </param>
 // <param name="validatedItems"> A dictionary keeping track of items that have been validated </param>
 private void ValidateComplexType(ComplexType item, List <EdmItemError> errors, HashSet <MetadataItem> validatedItems)
 {
     ValidateStructuralType(item, errors, validatedItems);
 }