public void TranslateColumnMap_returns_correct_columntypes_and_nullablecolumns_for_complex_types() { var metadataWorkspaceMock = new Mock <MetadataWorkspace>(); metadataWorkspaceMock.Setup(m => m.GetQueryCacheManager()).Returns(QueryCacheManager.Create()); var cSpaceComplexType = new ComplexType("C"); var intTypeUsage = TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32), new FacetValues { Nullable = false }); cSpaceComplexType.AddMember(new EdmProperty("Id", intTypeUsage)); cSpaceComplexType.AddMember(new EdmProperty("Count", intTypeUsage)); var complexTypeUsage = TypeUsage.Create(cSpaceComplexType); var recordMap = new ComplexTypeColumnMap( complexTypeUsage, "E", new[] { new ScalarColumnMap(cSpaceComplexType.Properties[0].TypeUsage, cSpaceComplexType.Properties[0].Name, 0, 0) }, new ScalarColumnMap(cSpaceComplexType.Properties[1].TypeUsage, cSpaceComplexType.Properties[1].Name, 0, 1)); var collectionMap = new SimpleCollectionColumnMap( complexTypeUsage, "MockCollectionType", recordMap, null, null); metadataWorkspaceMock.Setup(m => m.GetItem <EdmType>(It.IsAny <string>(), DataSpace.OSpace)) .Returns(new CodeFirstOSpaceTypeFactory().TryCreateType(typeof(SimpleEntity), cSpaceComplexType)); var factory = new Translator().TranslateColumnMap <object>( collectionMap, metadataWorkspaceMock.Object, new SpanIndex(), MergeOption.NoTracking, streaming: false, valueLayer: false); Assert.NotNull(factory); Assert.Equal(new[] { typeof(int), null }, factory.ColumnTypes); Assert.Equal(new[] { true, true }, factory.NullableColumns); }
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()); }
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.Complex("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 ComplexType_apply_should_set_given_value_for_unconfigured_strings() { var complexType = new ComplexType("C"); var property = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); complexType.AddMember(property); (new SqlCePropertyMaxLengthConvention(2000)).Apply(complexType, CreateDbModel()); Assert.Equal(2000, property.MaxLength); }
public static EdmProperty AddComplexProperty( this ComplexType complexType, string name, ComplexType targetComplexType) { EdmProperty complex = EdmProperty.CreateComplex(name, targetComplexType); complexType.AddMember((EdmMember)complex); return(complex); }
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 ComplexType_apply_should_set_correct_defaults_for_unconfigured_binary() { var entityType = new ComplexType("C"); var property = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property); ((IEdmConvention <ComplexType>) new SqlCePropertyMaxLengthConvention()) .Apply(entityType, CreateEdmModel()); Assert.Equal(4000, property.MaxLength); }
public void ComplexType_apply_should_set_given_value_for_non_unicode_fixed_length_strings() { var complexType = new ComplexType("C"); var property = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); property.IsFixedLength = true; property.IsUnicode = false; complexType.AddMember(property); (new SqlCePropertyMaxLengthConvention(2000)).Apply(complexType, CreateDbModel()); Assert.Equal(2000, property.MaxLength); }
public void ComplexType_apply_should_set_correct_defaults_for_fixed_length_binary() { var complexType = new ComplexType("C"); var property = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); property.IsFixedLength = true; complexType.AddMember(property); (new SqlCePropertyMaxLengthConvention()) .Apply(complexType, CreateDbModel()); Assert.Null(property.IsUnicode); Assert.Equal(4000, property.MaxLength); }
public void Map( PropertyInfo propertyInfo, ComplexType complexType, Func <ComplexTypeConfiguration> complexTypeConfiguration) { EdmProperty edmProperty = this.MapPrimitiveOrComplexOrEnumProperty(propertyInfo, (Func <StructuralTypeConfiguration>)complexTypeConfiguration, true); if (edmProperty == null) { return; } complexType.AddMember((EdmMember)edmProperty); }
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); }
private EdmModel CreateTestModel() { var model = new EdmModel(DataSpace.SSpace); var sqlManifest = new SqlProviderManifest("2008"); var stringTypeUsage = sqlManifest.GetStoreType( TypeUsage.CreateDefaultTypeUsage( PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String))); var complexType = new ComplexType("Entity", "Unicorns420", DataSpace.SSpace); complexType.AddMember(new EdmProperty("NullableProperty", stringTypeUsage) { Nullable = true }); complexType.AddMember(new EdmProperty("NonNullableProperty", stringTypeUsage) { Nullable = false }); model.AddItem(complexType); return(model); }
public void ComplexType_apply_should_set_correct_defaults_for_unconfigured_binary() { var entityType = new ComplexType("C"); var property = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Binary)); entityType.AddMember(property); ((IEdmConvention <ComplexType>) new PropertyMaxLengthConvention()) .Apply(entityType, new EdmModel(DataSpace.CSpace)); Assert.Null(property.IsUnicode); Assert.Equal(false, property.IsFixedLength); Assert.Null(property.MaxLength); }
public void ComplexType_apply_should_set_correct_defaults_for_non_unicode_fixed_length_strings() { var entityType = new ComplexType("C"); var property = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); property.IsFixedLength = true; property.IsUnicode = false; entityType.AddMember(property); ((IEdmConvention <ComplexType>) new SqlCePropertyMaxLengthConvention()) .Apply(entityType, CreateEdmModel()); Assert.Equal(4000, property.MaxLength); }
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.Complex(name, targetComplexType); complexType.AddMember(property); return(property); }
public void ComplexType_apply_should_set_correct_defaults_for_unicode_fixed_length_strings() { var entityType = new ComplexType("C"); var property = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); property.IsFixedLength = true; entityType.AddMember(property); ((IEdmConvention <ComplexType>) new PropertyMaxLengthConvention()) .Apply(entityType, new EdmModel(DataSpace.CSpace)); Assert.Equal(true, property.IsUnicode); Assert.Equal(128, property.MaxLength); Assert.False(property.IsMaxLength); }
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 ComplexType_apply_should_set_correct_defaults_for_unconfigured_strings() { var entityType = new ComplexType("C"); var property = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property); (new PropertyMaxLengthConvention()) .Apply(entityType, new DbModel(new EdmModel(DataSpace.CSpace), null)); Assert.Equal(true, property.IsUnicode); Assert.Equal(false, property.IsFixedLength); Assert.Null(property.MaxLength); Assert.Equal(true, property.IsMaxLength); }
public void ComplexType_apply_should_set_correct_defaults_for_fixed_length_binary() { var entityType = new ComplexType("C"); var property = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Binary)); property.IsFixedLength = true; entityType.AddMember(property); (new PropertyMaxLengthConvention()) .Apply(entityType, new DbModel(new EdmModel(DataSpace.CSpace), null)); Assert.Null(property.IsUnicode); Assert.Equal(128, property.MaxLength); Assert.False(property.IsMaxLength); }
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)); }
public void Generate_should_throw_when_circular_complex_property() { var functionParameterMappingGenerator = new FunctionParameterMappingGenerator(ProviderRegistry.Sql2008_ProviderManifest); var complexType = new ComplexType("CT", "N", DataSpace.CSpace); complexType.AddMember(EdmProperty.Complex("C1", complexType)); Assert.Equal( Strings.CircularComplexTypeHierarchy, Assert.Throws <InvalidOperationException>( () => functionParameterMappingGenerator .Generate( ModificationOperator.Insert, new[] { EdmProperty.Complex("C0", complexType) }, new ColumnMappingBuilder[0], new List <EdmProperty>()) .ToList()).Message); }
public virtual void Apply(EdmModel item, DbModel model) { Check.NotNull<EdmModel>(item, nameof (item)); Check.NotNull<DbModel>(model, nameof (model)); foreach (var data in item.EntityTypes.Where<EntityType>((Func<EntityType, bool>) (entityType => { if (entityType.KeyProperties.Count == 0) return entityType.BaseType == null; return false; })).Select(entityType => new { entityType = entityType, entityTypeConfiguration = entityType.GetConfiguration() as EntityTypeConfiguration }).Where(_param0 => { if (_param0.entityTypeConfiguration == null || !_param0.entityTypeConfiguration.IsExplicitEntity && _param0.entityTypeConfiguration.IsStructuralConfigurationOnly) return !_param0.entityType.Members.Where<EdmMember>(new Func<EdmMember, bool>(Helper.IsNavigationProperty)).Any<EdmMember>(); return false; }).Select(_param1 => new { \u003C\u003Eh__TransparentIdentifier0 = _param1, matchingAssociations = item.AssociationTypes.Where<AssociationType>((Func<AssociationType, bool>) (associationType => { if (associationType.SourceEnd.GetEntityType() != _param1.entityType) return associationType.TargetEnd.GetEntityType() == _param1.entityType; return true; })).Select(associationType => new { associationType = associationType, declaringEnd = associationType.SourceEnd.GetEntityType() == _param1.entityType ? associationType.SourceEnd : associationType.TargetEnd }).Select(_param0 => new { \u003C\u003Eh__TransparentIdentifier2 = _param0, declaringEntity = _param0.associationType.GetOtherEnd(_param0.declaringEnd).GetEntityType() }).Select(_param1 => new { \u003C\u003Eh__TransparentIdentifier3 = _param1, navigationProperties = _param1.declaringEntity.Members.Where<EdmMember>(new Func<EdmMember, bool>(Helper.IsNavigationProperty)).Cast<NavigationProperty>().Where<NavigationProperty>((Func<NavigationProperty, bool>) (n => n.ResultEnd.GetEntityType() == _param1.entityType)) }).Select(_param0 => new { DeclaringEnd = _param0.\u003C\u003Eh__TransparentIdentifier3.\u003C\u003Eh__TransparentIdentifier2.declaringEnd, AssociationType = _param0.\u003C\u003Eh__TransparentIdentifier3.\u003C\u003Eh__TransparentIdentifier2.associationType, DeclaringEntityType = _param0.\u003C\u003Eh__TransparentIdentifier3.declaringEntity, NavigationProperties = _param0.navigationProperties.ToList<NavigationProperty>() }) }).Where(_param0 => _param0.matchingAssociations.All(a => { if (a.AssociationType.Constraint == null && a.AssociationType.GetConfiguration() == null && (!a.AssociationType.IsSelfReferencing() && a.DeclaringEnd.IsOptional())) return a.NavigationProperties.All<NavigationProperty>((Func<NavigationProperty, bool>) (n => n.GetConfiguration() == null)); return false; })).Select(_param0 => new { EntityType = _param0.\u003C\u003Eh__TransparentIdentifier0.entityType, MatchingAssociations = _param0.matchingAssociations.ToList() }).ToList()) { ComplexType complexType = item.AddComplexType(data.EntityType.Name, data.EntityType.NamespaceName); foreach (EdmProperty declaredProperty in data.EntityType.DeclaredProperties) complexType.AddMember((EdmMember) declaredProperty); foreach (MetadataProperty annotation in data.EntityType.Annotations) complexType.GetMetadataProperties().Add(annotation); foreach (var matchingAssociation in data.MatchingAssociations) { foreach (NavigationProperty navigationProperty in matchingAssociation.NavigationProperties) { if (matchingAssociation.DeclaringEntityType.Members.Where<EdmMember>(new Func<EdmMember, bool>(Helper.IsNavigationProperty)).Contains<EdmMember>((EdmMember) navigationProperty)) { matchingAssociation.DeclaringEntityType.RemoveMember((EdmMember) navigationProperty); EdmProperty edmProperty = matchingAssociation.DeclaringEntityType.AddComplexProperty(navigationProperty.Name, complexType); foreach (MetadataProperty annotation in navigationProperty.Annotations) edmProperty.GetMetadataProperties().Add(annotation); } } item.RemoveAssociationType(matchingAssociation.AssociationType); } item.RemoveEntityType(data.EntityType); } }
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) } }); 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 <StorageConditionPropertyMapping>, List <StoragePropertyMapping> >( complexType, new List <StorageConditionPropertyMapping>(), new List <StoragePropertyMapping>()); structuralTypeMapping.Item3.Add(new StorageScalarPropertyMapping(complexTypeProperty1, rowTypeProperty1)); structuralTypeMapping.Item3.Add(new StorageScalarPropertyMapping(complexTypeProperty2, rowTypeProperty2)); var functionImportMapping = new FunctionImportMappingComposable( functionImport, storeFunction, new List <Tuple <StructuralType, List <StorageConditionPropertyMapping>, List <StoragePropertyMapping> > > { structuralTypeMapping }); var containerMapping = new StorageEntityContainerMapping(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()); }