public void EdmFunction_DuplicateParameterName() { var validationContext = new EdmModelValidationContext(new EdmModel(DataSpace.SSpace), true); DataModelErrorEventArgs errorEventArgs = null; validationContext.OnError += (_, e) => errorEventArgs = e; var parameter1 = new FunctionParameter( "P", TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)), ParameterMode.In); var parameter2 = new FunctionParameter( "P2", TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)), ParameterMode.In); var function = new EdmFunction( "F", "N", DataSpace.SSpace, new EdmFunctionPayload { Parameters = new[] { parameter1, parameter2 } }); parameter2.Name = "P"; EdmModelSemanticValidationRules .EdmFunction_DuplicateParameterName .Evaluate(validationContext, function); Assert.NotNull(errorEventArgs); Assert.Same(parameter2, errorEventArgs.Item); Assert.Equal( Strings.ParameterNameAlreadyDefinedDuplicate("P"), errorEventArgs.ErrorMessage); }
public static TypeUsage TypeUsageForPrimitiveType(Type type, ObjectContext objectContext) { bool isNullable = IsNullableType(type); type = PrimitiveTypeForType(type); // Find equivalent EdmType in CSpace. This is a 1-to-1 mapping to CLR types except for the Geometry/Geography types // (so not supporting those atm). var primitiveTypeList = objectContext.MetadataWorkspace .GetPrimitiveTypes(DataSpace.CSpace) .Where(p => p.ClrEquivalentType == type) .ToList(); if (primitiveTypeList.Count != 1) { throw new ApplicationException(string.Format("Unable to map parameter of type {0} to TypeUsage. Found {1} matching types", type.Name, primitiveTypeList.Count)); } var primitiveType = primitiveTypeList.FirstOrDefault(); var facetList = new List <Facet>(); if (isNullable) { // May not even be necessary to specify these Facets, but just to be safe. And only way to create them is to call the internal Create method... var createMethod = typeof(Facet).GetMethod("Create", BindingFlags.NonPublic | BindingFlags.Static, null, new Type[] { typeof(FacetDescription), typeof(object) }, null); var facetDescription = Facet.GetGeneralFacetDescriptions().FirstOrDefault(fd => fd.FacetName == "Nullable"); if (facetDescription != null) { facetList.Add((Facet)createMethod.Invoke(null, new object[] { facetDescription, true })); } facetDescription = Facet.GetGeneralFacetDescriptions().FirstOrDefault(fd => fd.FacetName == "DefaultValue"); if (facetDescription != null) { facetList.Add((Facet)createMethod.Invoke(null, new object[] { facetDescription, null })); } } return(TypeUsage.Create(primitiveType, facetList)); }
private static ScalarColumnMap[] CreateDiscriminatorColumnMaps(DbDataReader storeDataReader, FunctionImportMappingNonComposable mapping, int resultIndex) { // choose an arbitrary type for discriminator columns -- the type is not // actually statically known EdmType discriminatorType = MetadataItem.EdmProviderManifest.GetPrimitiveType(PrimitiveTypeKind.String); TypeUsage discriminatorTypeUsage = TypeUsage.Create(discriminatorType); IList <string> discriminatorColumnNames = mapping.GetDiscriminatorColumns(resultIndex); ScalarColumnMap[] discriminatorColumns = new ScalarColumnMap[discriminatorColumnNames.Count]; for (int i = 0; i < discriminatorColumns.Length; i++) { string columnName = discriminatorColumnNames[i]; ScalarColumnMap columnMap = new ScalarColumnMap(discriminatorTypeUsage, columnName, 0, GetDiscriminatorOrdinalFromReader(storeDataReader, columnName, mapping.FunctionImport)); discriminatorColumns[i] = columnMap; } return(discriminatorColumns); }
public void SimpleNullableTypeShouldBeEscaped() { var primitiveType = PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32); // XXX: // I'm stupid and/or wrong. Why do I have to jump through these hoops // to validate this code!?! var property = EdmProperty.CreatePrimitive("prop", primitiveType); property.Nullable = true; var nullableFacet = property .MetadataProperties .SelectMany(x => x.TypeUsage.Facets) .First(x => x.Name == System.Data.Entity.Core.Common.DbProviderManifest.NullableFacetName); var typeUsage = TypeUsage.Create(primitiveType, new[] { nullableFacet }); this.escaper.Escape(typeUsage).Should().Be("global::System.Nullable<global::System.Int32>"); // XXX }
internal EdmProviderManifestFunctionBuilder(ReadOnlyCollection <PrimitiveType> edmPrimitiveTypes) { Debug.Assert(edmPrimitiveTypes != null, "Primitive types should not be null"); // Initialize all the various parameter types. We do not want to create new instance of parameter types // again and again for perf reasons var primitiveTypeUsages = new TypeUsage[edmPrimitiveTypes.Count]; foreach (var edmType in edmPrimitiveTypes) { Debug.Assert( (int)edmType.PrimitiveTypeKind < primitiveTypeUsages.Length && (int)edmType.PrimitiveTypeKind >= 0, "Invalid PrimitiveTypeKind value?"); Debug.Assert( primitiveTypeUsages[(int)edmType.PrimitiveTypeKind] == null, "Duplicate PrimitiveTypeKind value in EDM primitive types?"); primitiveTypeUsages[(int)edmType.PrimitiveTypeKind] = TypeUsage.Create(edmType); } primitiveTypes = primitiveTypeUsages; }
public void Configure_should_split_key_constraint_when_to_table_configuration() { var database = new EdmModel().DbInitialize(); var sourceTable = database.AddTable("Source"); var principalTable = database.AddTable("P"); var fkColumn = new EdmProperty( "Fk", ProviderRegistry.Sql2008_ProviderManifest.GetStoreType( TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)))); sourceTable.AddColumn(fkColumn); var foreignKeyConstraint = new ForeignKeyBuilder(database, "FK") { PrincipalTable = principalTable }; sourceTable.AddForeignKey(foreignKeyConstraint); foreignKeyConstraint.DependentColumns = new[] { fkColumn }; var targetTable = database.AddTable("Split"); var associationSetMapping = new StorageAssociationSetMapping( new AssociationSet("AS", new AssociationType()), database.GetEntitySet(sourceTable)).Initialize(); associationSetMapping.SourceEndMapping.AddProperty(new StorageScalarPropertyMapping(new EdmProperty("PK"), fkColumn)); var independentAssociationMappingConfiguration = new ForeignKeyAssociationMappingConfiguration(); independentAssociationMappingConfiguration.ToTable("Split"); independentAssociationMappingConfiguration.Configure(associationSetMapping, database, new MockPropertyInfo()); Assert.True(targetTable.Properties.Contains(fkColumn)); Assert.True(targetTable.ForeignKeyBuilders.Contains(foreignKeyConstraint)); Assert.False(sourceTable.Properties.Contains(fkColumn)); Assert.False(sourceTable.ForeignKeyBuilders.Contains(foreignKeyConstraint)); Assert.Same(targetTable, associationSetMapping.Table); }
public void Can_configure_result_bindings() { var modificationFunctionConfiguration = new ModificationFunctionConfiguration(); var mockPropertyInfo = new MockPropertyInfo(); modificationFunctionConfiguration .Result(new PropertyPath(mockPropertyInfo), "Foo"); var entitySet = new EntitySet(); entitySet.ChangeEntityContainerWithoutCollectionFixup(new EntityContainer("C", DataSpace.CSpace)); var property = new EdmProperty("P1"); property.SetClrPropertyInfo(mockPropertyInfo); var resultBinding = new StorageModificationFunctionResultBinding("Bar", property); modificationFunctionConfiguration.Configure( new StorageModificationFunctionMapping( entitySet, new EntityType("E", "N", DataSpace.CSpace), new EdmFunction("F", "N", DataSpace.SSpace), new[] { new StorageModificationFunctionParameterBinding( new FunctionParameter( "P", TypeUsage.Create( PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)), ParameterMode.In), new StorageModificationFunctionMemberPath(new[] { property }, null), false) }, null, new[] { resultBinding }), ProviderRegistry.Sql2008_ProviderManifest); Assert.Equal("Foo", resultBinding.ColumnName); }
public void TranslateColumnMap_returns_correct_columntypes_and_nullablecolumns_for_discriminated_collections() { var metadataWorkspaceMock = new Mock <MetadataWorkspace>(); metadataWorkspaceMock.Setup(m => m.GetQueryCacheManager()).Returns(QueryCacheManager.Create()); var entityColumnMap = (EntityColumnMap)BuildSimpleEntitySetColumnMap(metadataWorkspaceMock).Element; var collectionMap = new DiscriminatedCollectionColumnMap( entityColumnMap.Type, "MockCollectionType", entityColumnMap, null, null, new ScalarColumnMap(TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Boolean)), "discriminator", 0, 2), true); 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), typeof(int), typeof(bool) }, factory.ColumnTypes); Assert.Equal(new[] { true, true, true }, factory.NullableColumns); }
public void Can_configure_rows_affected_parameter_name() { var modificationFunctionConfiguration = new ModificationStoredProcedureConfiguration(); var mockPropertyInfo = new MockPropertyInfo(); modificationFunctionConfiguration.RowsAffectedParameter("Foo"); var entitySet = new EntitySet(); entitySet.ChangeEntityContainerWithoutCollectionFixup(new EntityContainer("C", DataSpace.CSpace)); var property = new EdmProperty("P1"); property.SetClrPropertyInfo(mockPropertyInfo); var rowsAffectedParameter = new FunctionParameter(); modificationFunctionConfiguration.Configure( new ModificationFunctionMapping( entitySet, new EntityType("E", "N", DataSpace.CSpace), new EdmFunction("F", "N", DataSpace.SSpace), new[] { new ModificationFunctionParameterBinding( new FunctionParameter( "P", TypeUsage.Create( PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)), ParameterMode.In), new ModificationFunctionMemberPath(new[] { property }, null), false) }, rowsAffectedParameter, null), ProviderRegistry.Sql2008_ProviderManifest); Assert.Equal("Foo", rowsAffectedParameter.Name); }
internal static void PopulateParameterFromTypeUsage( EntityParameter parameter, TypeUsage type, bool isOutParam) { if (type != null) { if (Helper.IsEnumType(type.EdmType)) { type = TypeUsage.Create((EdmType)Helper.GetUnderlyingEdmTypeForEnumType(type.EdmType)); } else { PrimitiveTypeKind spatialType; if (Helper.IsSpatialType(type, out spatialType)) { parameter.EdmType = (EdmType)EdmProviderManifest.Instance.GetPrimitiveType(spatialType); } } } DbCommandDefinition.PopulateParameterFromTypeUsage((DbParameter)parameter, type, isOutParam); }
public void Configure_should_not_validate_consistency_of_dependent_end_when_both_unconfigured() { var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace); associationType.SourceEnd = new AssociationEndMember("S", new EntityType("E", "N", DataSpace.CSpace)); associationType.TargetEnd = new AssociationEndMember("T", new EntityType("E", "N", DataSpace.CSpace)); var navigationPropertyConfigurationA = new NavigationPropertyConfiguration(new MockPropertyInfo(typeof(AType1), "N")); associationType.SetConfiguration(navigationPropertyConfigurationA); var navigationPropertyConfigurationB = new NavigationPropertyConfiguration(new MockPropertyInfo(typeof(AType1), "N")); navigationPropertyConfigurationB.Configure( new NavigationProperty("N", TypeUsage.Create(associationType.TargetEnd.GetEntityType())) { RelationshipType = associationType }, new EdmModel(DataSpace.CSpace), new EntityTypeConfiguration(typeof(object))); }
internal override bool ResolveNameAndSetTypeUsage( Converter.ConversionCache convertedItemCache, Dictionary <SchemaElement, GlobalItem> newGlobalItems) { if (this._typeUsage != null) { return(true); } if (this._typeSubElement != null) { return(this._typeSubElement.ResolveNameAndSetTypeUsage(convertedItemCache, newGlobalItems)); } if (this._type is ScalarType) { this._typeUsageBuilder.ValidateAndSetTypeUsage(this._type as ScalarType, false); this._typeUsage = this._typeUsageBuilder.TypeUsage; } else { EdmType edmType = (EdmType)Converter.LoadSchemaElement(this._type, this._type.Schema.ProviderManifest, convertedItemCache, newGlobalItems); if (edmType != null) { if (this._isRefType) { this._typeUsage = TypeUsage.Create((EdmType) new RefType(edmType as System.Data.Entity.Core.Metadata.Edm.EntityType)); } else { this._typeUsageBuilder.ValidateAndSetTypeUsage(edmType, false); this._typeUsage = this._typeUsageBuilder.TypeUsage; } } } if (this._collectionKind != CollectionKind.None) { this._typeUsage = TypeUsage.Create((EdmType) new CollectionType(this._typeUsage)); } return(this._typeUsage != null); }
public void Setting_column_should_update_property_mapping() { var columnProperty1 = new EdmProperty("C1", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })); var property = new EdmProperty("P"); var columnMappingBuilder = new ColumnMappingBuilder(columnProperty1, new[] { property }); var scalarPropertyMapping = new ScalarPropertyMapping(property, columnProperty1); columnMappingBuilder.SetTarget(scalarPropertyMapping); var columnProperty2 = new EdmProperty("C2", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })); columnMappingBuilder.ColumnProperty = columnProperty2; Assert.Same(columnProperty2, columnMappingBuilder.ColumnProperty); Assert.Same(columnProperty2, scalarPropertyMapping.Column); }
public void Configure_should_configure_ends() { var navigationPropertyConfiguration = new NavigationPropertyConfiguration(new MockPropertyInfo(typeof(AType1), "N")) { RelationshipMultiplicity = RelationshipMultiplicity.ZeroOrOne, InverseEndKind = RelationshipMultiplicity.Many }; var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace); associationType.SourceEnd = new AssociationEndMember("S", new EntityType("E", "N", DataSpace.CSpace)); associationType.TargetEnd = new AssociationEndMember("T", new EntityType("E", "N", DataSpace.CSpace)); navigationPropertyConfiguration.Configure( new NavigationProperty("N", TypeUsage.Create(associationType.TargetEnd.GetEntityType())) { RelationshipType = associationType }, new EdmModel(DataSpace.CSpace), new EntityTypeConfiguration(typeof(object))); Assert.Equal(RelationshipMultiplicity.Many, associationType.SourceEnd.RelationshipMultiplicity); Assert.Equal(RelationshipMultiplicity.ZeroOrOne, associationType.TargetEnd.RelationshipMultiplicity); }
public void Configure_should_ensure_consistency_of_constraint_when_already_configured() { var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace); associationType.SourceEnd = new AssociationEndMember("S", new EntityType("SE", "N", DataSpace.CSpace)); var targetEntityType = new EntityType("TE", "N", DataSpace.CSpace); var propertyInfo = new MockPropertyInfo(typeof(int), "P2").Object; var property = new EdmProperty("P2", TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32))); targetEntityType.AddMember(property); property.SetClrPropertyInfo(propertyInfo); associationType.TargetEnd = new AssociationEndMember("T", targetEntityType); var navigationPropertyConfigurationA = new NavigationPropertyConfiguration(new MockPropertyInfo(typeof(AType1), "N1")); associationType.SetConfiguration(navigationPropertyConfigurationA); var constraint = new ForeignKeyConstraintConfiguration( new[] { propertyInfo }); var navigationPropertyConfigurationB = new NavigationPropertyConfiguration(new MockPropertyInfo(typeof(AType1), "N2")) { Constraint = constraint }; navigationPropertyConfigurationB.Configure( new NavigationProperty("N", TypeUsage.Create(associationType.TargetEnd.GetEntityType())) { RelationshipType = associationType }, new EdmModel(DataSpace.CSpace), new EntityTypeConfiguration(typeof(object))); Assert.Equal(constraint, navigationPropertyConfigurationA.Constraint); }
/// <summary> /// Build the collectionColumnMap from a store datareader, a type and an entitySet. /// </summary> /// <param name="storeDataReader"></param> /// <param name="edmType"></param> /// <param name="entitySet"></param> /// <returns></returns> internal static CollectionColumnMap CreateColumnMapFromReaderAndType(DbDataReader storeDataReader, EdmType edmType, EntitySet entitySet, Dictionary <string, FunctionImportReturnTypeStructuralTypeColumnRenameMapping> renameList) { Debug.Assert(Helper.IsEntityType(edmType) || null == entitySet, "The specified non-null EntitySet is incompatible with the EDM type specified."); // Next, build the ColumnMap directly from the edmType and entitySet provided. ColumnMap[] propertyColumnMaps = GetColumnMapsForType(storeDataReader, edmType, renameList); ColumnMap elementColumnMap = null; // NOTE: We don't have a null sentinel here, because the stored proc won't // return one anyway; we'll just presume the data's always there. if (Helper.IsRowType(edmType)) { elementColumnMap = new RecordColumnMap(TypeUsage.Create(edmType), edmType.Name, propertyColumnMaps, null); } else if (Helper.IsComplexType(edmType)) { elementColumnMap = new ComplexTypeColumnMap(TypeUsage.Create(edmType), edmType.Name, propertyColumnMaps, null); } else if (Helper.IsScalarType(edmType)) { if (storeDataReader.FieldCount != 1) { throw EntityUtil.CommandExecutionDataReaderFieldCountForScalarType(); } elementColumnMap = new ScalarColumnMap(TypeUsage.Create(edmType), edmType.Name, 0, 0); } else if (Helper.IsEntityType(edmType)) { elementColumnMap = CreateEntityTypeElementColumnMap(storeDataReader, edmType, entitySet, propertyColumnMaps, null /*renameList*/); } else { Debug.Assert(false, "unexpected edmType?"); } CollectionColumnMap collection = new SimpleCollectionColumnMap(edmType.GetCollectionType().TypeUsage, edmType.Name, elementColumnMap, null, null); return(collection); }
public void Can_update_complex_column_mapping() { var mappingFragment = new MappingFragment( new EntitySet(), new EntityTypeMapping( new EntitySetMapping( new EntitySet(), new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)))), false); var property1 = EdmProperty.CreateComplex("P1", new ComplexType("CT")); var property2 = new EdmProperty("P2"); var columnMappingBuilder1 = new ColumnMappingBuilder(new EdmProperty("C", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })), new[] { property1, property2 }); mappingFragment.AddColumnMapping(columnMappingBuilder1); var columnProperty = new EdmProperty("C", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })); var columnMappingBuilder2 = new ColumnMappingBuilder(columnProperty, new[] { property1, property2 }); mappingFragment.AddColumnMapping(columnMappingBuilder2); var complexPropertyMapping = (ComplexPropertyMapping)mappingFragment.PropertyMappings.Single(); var typeMapping = complexPropertyMapping.TypeMappings.Single(); var scalarPropertyMapping = (ScalarPropertyMapping)typeMapping.PropertyMappings.Single(); Assert.Same(columnProperty, scalarPropertyMapping.Column); Assert.Same(property2, scalarPropertyMapping.Property); }
/// <summary> /// Get the type usage for this parameter in model terms. /// </summary> /// <returns> The type usage for this parameter </returns> /// <remarks> /// Because GetTypeUsage throws CommandValidationExceptions, it should only be called from EntityCommand during command execution /// </remarks> internal virtual TypeUsage GetTypeUsage() { TypeUsage typeUsage; if (!IsTypeConsistent) { throw new InvalidOperationException( Strings.EntityClient_EntityParameterInconsistentEdmType( _edmType.FullName, _parameterName)); } if (_edmType != null) { typeUsage = TypeUsage.Create(_edmType); } else if (!DbTypeMap.TryGetModelTypeUsage(DbType, out typeUsage)) { // Spatial types have only DbType 'Object', and cannot be represented in the static type map. PrimitiveType primitiveParameterType; if (DbType == DbType.Object && Value != null && ClrProviderManifest.Instance.TryGetPrimitiveType(Value.GetType(), out primitiveParameterType) && Helper.IsSpatialType(primitiveParameterType)) { typeUsage = EdmProviderManifest.Instance.GetCanonicalModelTypeUsage(primitiveParameterType.PrimitiveTypeKind); } else { throw new InvalidOperationException(Strings.EntityClient_UnsupportedDbType(DbType.ToString(), ParameterName)); } } Debug.Assert(typeUsage != null, "DbType.TryGetModelTypeUsage returned true for null TypeUsage?"); return(typeUsage); }
public void Can_add_get_remove_column_conditions() { var entitySet1 = new EntitySet(); var associationSet = new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace)); var associationSetMapping = new AssociationSetMapping(associationSet, entitySet1); Assert.Empty(associationSetMapping.Conditions); var conditionPropertyMapping = new ValueConditionMapping(new EdmProperty("C", TypeUsage.Create(new PrimitiveType { DataSpace = DataSpace.SSpace })), 42); associationSetMapping.AddCondition(conditionPropertyMapping); Assert.Same(conditionPropertyMapping, associationSetMapping.Conditions.Single()); associationSetMapping.RemoveCondition(conditionPropertyMapping); Assert.Empty(associationSetMapping.Conditions); }
public void Cannot_add_duplicate_column_mapping_builder() { var mappingFragment = new MappingFragment( new EntitySet(), new EntityTypeMapping( new EntitySetMapping( new EntitySet(), new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)))), false); var columnMappingBuilder = new ColumnMappingBuilder(new EdmProperty("S", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })), new[] { new EdmProperty("S") }); mappingFragment.AddColumnMapping(columnMappingBuilder); Assert.Equal( Strings.InvalidColumnBuilderArgument("columnBuilderMapping"), Assert.Throws <ArgumentException>( () => mappingFragment.AddColumnMapping(columnMappingBuilder)).Message); }
public static void GetIdentity_of_StorageComplexTypeMapping_returns_expected_value() { var complexType1 = new ComplexType("CT1", "N", DataSpace.CSpace); var complexType2 = new ComplexType("CT2", "N", DataSpace.CSpace); var complexType3 = new ComplexType("CT3", "N", DataSpace.CSpace); var complexType4 = new ComplexType("CT4", "N", DataSpace.CSpace); var property1 = new EdmProperty("A", TypeUsage.Create(complexType1)); var property2 = new EdmProperty("B", TypeUsage.Create(complexType2)); var propertyMapping1 = new ComplexPropertyMapping(property1); var propertyMapping2 = new ComplexPropertyMapping(property2); var mapping = new ComplexTypeMapping(false); mapping.AddType(complexType2); mapping.AddType(complexType1); mapping.AddIsOfType(complexType4); mapping.AddIsOfType(complexType3); mapping.AddPropertyMapping(propertyMapping2); mapping.AddPropertyMapping(propertyMapping1); Assert.Equal("ComplexProperty(Identity=A),ComplexProperty(Identity=B),N.CT1,N.CT2,N.CT3,N.CT4", BaseMetadataMappingVisitor.IdentityHelper.GetIdentity(mapping)); }
internal virtual TypeUsage GetTypeUsage() { if (!this.IsTypeConsistent) { throw new InvalidOperationException(Strings.EntityClient_EntityParameterInconsistentEdmType((object)this._edmType.FullName, (object)this._parameterName)); } TypeUsage modelType; if (this._edmType != null) { modelType = TypeUsage.Create(this._edmType); } else if (!DbTypeMap.TryGetModelTypeUsage(this.DbType, out modelType)) { PrimitiveType primitiveType; if (this.DbType != DbType.Object || this.Value == null || (!ClrProviderManifest.Instance.TryGetPrimitiveType(this.Value.GetType(), out primitiveType) || !Helper.IsSpatialType(primitiveType))) { throw new InvalidOperationException(Strings.EntityClient_UnsupportedDbType((object)this.DbType.ToString(), (object)this.ParameterName)); } modelType = EdmProviderManifest.Instance.GetCanonicalModelTypeUsage(primitiveType.PrimitiveTypeKind); } return(modelType); }
static internal TypeUsage GetLiteralTypeUsage(PrimitiveTypeKind primitiveTypeKind, bool isUnicode) { TypeUsage typeusage; PrimitiveType primitiveType = EdmProviderManifest.Instance.GetPrimitiveType(primitiveTypeKind); switch (primitiveTypeKind) { case PrimitiveTypeKind.String: typeusage = TypeUsage.Create(primitiveType, new FacetValues { Unicode = isUnicode, MaxLength = TypeUsage.DefaultMaxLengthFacetValue, FixedLength = false, Nullable = false }); break; default: typeusage = TypeUsage.Create(primitiveType, new FacetValues { Nullable = false }); break; } return(typeusage); }
public void Configure_should_update_model_dateTime_precision() { var configuration = CreateConfiguration(); configuration.Precision = 255; var property = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.DateTime)); configuration.Configure(property); Assert.Equal((byte)255, property.Precision); var edmPropertyMapping = new ColumnMappingBuilder( new EdmProperty( "C", TypeUsage.Create(ProviderRegistry.Sql2008_ProviderManifest.GetStoreTypes().First(t => t.Name == "datetime2"))), new List <EdmProperty>()); configuration.Configure( new[] { Tuple.Create(edmPropertyMapping, new EntityType("T", XmlConstants.TargetNamespace_3, DataSpace.SSpace)) }, ProviderRegistry.Sql2008_ProviderManifest); Assert.Equal((byte)255, edmPropertyMapping.ColumnProperty.Precision); }
public static void SetStoreGeneratedPattern( this EdmProperty property, StoreGeneratedPattern storeGeneratedPattern) { DebugCheck.NotNull(property); MetadataProperty metadataProperty; if (!property.MetadataProperties.TryGetValue( XmlConstants.StoreGeneratedPatternAnnotation, false, out metadataProperty)) { property.MetadataProperties.Source.Add( new MetadataProperty( XmlConstants.StoreGeneratedPatternAnnotation, TypeUsage.Create(EdmProviderManifest.Instance.GetPrimitiveType(PrimitiveTypeKind.String)), storeGeneratedPattern.ToString())); } else { metadataProperty.Value = storeGeneratedPattern.ToString(); } }
internal override DbExpression AsCqt(DbExpression row, bool skipIsNotNull) { DbExpression cqt = this.RestrictedMemberSlot.MemberPath.AsCqt(row); if (Helper.IsRefType(this.RestrictedMemberSlot.MemberPath.EdmType)) { cqt = cqt.Deref(); } if (this.Domain.Count == 1) { // Single value cqt = cqt.IsOfOnly(TypeUsage.Create(((TypeConstant)this.Domain.Values.Single()).EdmType)); } else { // Multiple values: build list of var IsOnOnly(t1), var = IsOnOnly(t1), ..., then OR them all. List <DbExpression> operands = this.Domain.Values.Select(t => (DbExpression)cqt.IsOfOnly(TypeUsage.Create(((TypeConstant)t).EdmType))).ToList(); cqt = Helpers.BuildBalancedTreeInPlace(operands, (prev, next) => prev.Or(next)); } return(cqt); }
public void Configure_should_update_IsUnicode() { var configuration = CreateConfiguration(); configuration.IsUnicode = true; var property = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); configuration.Configure(property); Assert.Equal(true, property.IsUnicode); var edmPropertyMapping = new ColumnMappingBuilder( new EdmProperty( "C", TypeUsage.Create(ProviderRegistry.Sql2008_ProviderManifest.GetStoreTypes().First(t => t.Name == "nvarchar"))), new List <EdmProperty>()); configuration.Configure( new[] { Tuple.Create(edmPropertyMapping, new EntityType("T", XmlConstants.TargetNamespace_3, DataSpace.SSpace)) }, ProviderRegistry.Sql2008_ProviderManifest); Assert.Equal(true, edmPropertyMapping.ColumnProperty.IsUnicode); }
private Dictionary <EdmProperty, TypeUsage> FindStoreTypeUsages(EntityType entityType) { Debug.Assert(entityType != null, "entityType == null"); var propertyToStoreTypeUsage = new Dictionary <EdmProperty, TypeUsage>(); var types = Tools.GetTypeHierarchy(entityType); var entityTypeMappings = _model.ConceptualToStoreMapping.EntitySetMappings .SelectMany(s => s.EntityTypeMappings) .Where(t => types.Contains(t.EntityType)) .ToArray(); foreach (var property in entityType.Properties) { foreach (var entityTypeMapping in entityTypeMappings) { var propertyMapping = (ScalarPropertyMapping)entityTypeMapping.Fragments.SelectMany(f => f.PropertyMappings) .FirstOrDefault(p => p.Property == property); if (propertyMapping != null) { Debug.Assert(!propertyToStoreTypeUsage.ContainsKey(property), "Property already in dictionary"); propertyToStoreTypeUsage[property] = TypeUsage.Create( propertyMapping.Column.TypeUsage.EdmType, propertyMapping.Column.TypeUsage.Facets.Where( f => f.Name != "StoreGeneratedPattern" && f.Name != "ConcurrencyMode")); break; } } } return(propertyToStoreTypeUsage); }
private RowType CreateRowTypeFromEntityType(EntityType entityType) { Debug.Assert(entityType != null, "entityType == null"); var types = Tools.GetTypeHierarchy(entityType); var entityTypeMappings = _model.ConceptualToStoreMapping.EntitySetMappings .SelectMany(s => s.EntityTypeMappings) .Where(t => types.Contains(t.EntityType)) .ToArray(); List <EdmProperty> rowTypeProperties = new List <EdmProperty>(); foreach (var property in entityType.Properties) { foreach (var entityTypeMapping in entityTypeMappings) { var propertyMapping = (ScalarPropertyMapping)entityTypeMapping.Fragments.SelectMany(f => f.PropertyMappings) .FirstOrDefault(p => p.Property == property); if (propertyMapping != null) { // we must use the column name and not just the name of the property to support custom column mappings rowTypeProperties.Add(EdmProperty.Create(propertyMapping.Column.Name, TypeUsage.Create( propertyMapping.Column.TypeUsage.EdmType, propertyMapping.Column.TypeUsage.Facets.Where( f => f.Name != "StoreGeneratedPattern" && f.Name != "ConcurrencyMode")))); break; } } } return(RowType.Create(rowTypeProperties, null)); }
public void Configure_should_validate_consistency_of_constraint_when_already_configured() { var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace); associationType.SourceEnd = new AssociationEndMember("S", new EntityType("E", "N", DataSpace.CSpace)); associationType.TargetEnd = new AssociationEndMember("T", new EntityType("E", "N", DataSpace.CSpace)); var navigationPropertyConfigurationA = new NavigationPropertyConfiguration(new MockPropertyInfo(typeof(AType1), "N1")) { Constraint = new ForeignKeyConstraintConfiguration( new[] { new MockPropertyInfo(typeof(int), "P1").Object }) }; associationType.SetConfiguration(navigationPropertyConfigurationA); var navigationPropertyConfigurationB = new NavigationPropertyConfiguration(new MockPropertyInfo(typeof(AType1), "N2")) { Constraint = new ForeignKeyConstraintConfiguration( new[] { new MockPropertyInfo(typeof(int), "P2").Object }) }; Assert.Equal( Strings.ConflictingConstraint("N2", typeof(object)), Assert.Throws <InvalidOperationException>( () => navigationPropertyConfigurationB.Configure( new NavigationProperty("N", TypeUsage.Create(associationType.TargetEnd.GetEntityType())) { RelationshipType = associationType }, new EdmModel(DataSpace.CSpace), new EntityTypeConfiguration(typeof(object)))).Message); }