{"record":{"id":"1b8e15c0f4593569","repo":"dotnet/efcore","slug":"the-value-value-provided-for-argument-argume-1b8e15","errorCode":null,"errorMessage":"The value '{value}' provided for argument '{argumentName}' must be a valid value of enum type '{enumType}'.","messagePattern":"The value '(.+?)' provided for argument '(.+?)' must be a valid value of enum type '(.+?)'\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Extensions/CosmosPropertyBuilderExtensions.cs","lineNumber":212,"sourceCode":"    /// <returns><see langword=\"true\" /> if the vector distance function and dimensions can be set.</returns>\n    public static bool CanSetIsVectorProperty(\n        this IConventionPropertyBuilder propertyBuilder,\n        DistanceFunction distanceFunction,\n        int dimensions,\n        bool fromDataAnnotation = false)\n        => propertyBuilder.CanSetAnnotation(\n                CosmosAnnotationNames.VectorDistanceFunction,\n                ValidateVectorDistanceFunction(distanceFunction),\n                fromDataAnnotation)\n            && propertyBuilder.CanSetAnnotation(\n                CosmosAnnotationNames.VectorDimensions,\n                dimensions,\n                fromDataAnnotation);\n\n    private static DistanceFunction ValidateVectorDistanceFunction(DistanceFunction distanceFunction)\n        => Enum.IsDefined(distanceFunction)\n            ? distanceFunction\n            : throw new ArgumentException(\n                CoreStrings.InvalidEnumValue(\n                    distanceFunction,\n                    nameof(distanceFunction),\n                    typeof(DistanceFunction)));\n\n    /// <summary>\n    ///     Configures this property to be the etag concurrency token.\n    /// </summary>\n    /// <remarks>\n    ///     See <see href=\"https://aka.ms/efcore-docs-modeling\">Modeling entity types and relationships</see>, and\n    ///     <see href=\"https://aka.ms/efcore-docs-cosmos\">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.\n    /// </remarks>\n    /// <param name=\"propertyBuilder\">The builder for the property being configured.</param>\n    /// <returns>The same builder instance so that multiple calls can be chained.</returns>\n    public static PropertyBuilder IsETagConcurrency(this PropertyBuilder propertyBuilder)\n    {\n        propertyBuilder\n            .IsConcurrencyToken()","sourceCodeStart":194,"sourceCodeEnd":230,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Extensions/CosmosPropertyBuilderExtensions.cs#L194-L230","documentation":"CosmosPropertyBuilderExtensions.ValidateVectorDistanceFunction throws ArgumentException(CoreStrings.InvalidEnumValue) when an undefined DistanceFunction enum value is passed to IsVectorProperty or CanSetIsVectorProperty. The check is Enum.IsDefined(distanceFunction); any value not declared on DistanceFunction (e.g. an explicit cast from an arbitrary int, or a future enum value removed after upgrade) is rejected at configuration time, before any annotation is set.","triggerScenarios":"Casting an arbitrary integer to DistanceFunction and passing it: builder.IsVectorProperty((DistanceFunction)42, 1536); reading a distance function from configuration as an int and casting without validation; downgrading EF Core where a DistanceFunction member was removed so a serialized value no longer maps to a defined member; passing default(DistanceFunction) when default is not a defined member.","commonSituations":"Loading distance function from a settings file as int and casting directly; sharing enums across versions; reflection-based model configuration that bypasses compile-time enum checks; upgrading EF Core and finding a previously-valid DistanceFunction value is no longer defined.","solutions":["Validate the value with Enum.IsDefined(typeof(DistanceFunction), value) before casting or calling IsVectorProperty, and fall back to a sensible default (e.g. DistanceFunction.Cosine) if it is not defined.","Bind configuration as a named string and parse with Enum.TryParse<DistanceFunction>(name, out var df) so unknown values are caught at parse time.","If upgrading EF Core, check the DistanceFunction members in the version you target and remap any removed value.","Avoid (DistanceFunction)someInt casts at call sites; route through a validation helper."],"exampleFix":"// before - throws when cfg.DistanceFunctionInt is not a defined enum value\nentityBuilder.Property(d => d.Embedding)\n    .IsVectorProperty((DistanceFunction)cfg.DistanceFunctionInt, cfg.Dimensions);\n\n// after - validate first\nvar df = Enum.IsDefined(typeof(DistanceFunction), cfg.DistanceFunctionInt)\n    ? (DistanceFunction)cfg.DistanceFunctionInt\n    : DistanceFunction.Cosine;\nentityBuilder.Property(d => d.Embedding)\n    .IsVectorProperty(df, cfg.Dimensions);","handlingStrategy":"validation","validationCode":"// Validate the enum before configuring the vector property.\nDistanceFunction SafeDistance(DistanceFunction df)\n    => Enum.IsDefined(typeof(DistanceFunction), df)\n        ? df\n        : throw new ArgumentOutOfRangeException(nameof(df), df, \"Undefined DistanceFunction value.\");\n\nbuilder.Property(d => d.Embedding).IsVectorProperty(SafeDistance(distanceFunction), dimensions);","typeGuard":"// If the value comes in as int from configuration, validate first.\nstatic DistanceFunction ParseDistance(int raw, DistanceFunction fallback = DistanceFunction.Cosine)\n    => Enum.IsDefined(typeof(DistanceFunction), raw) ? (DistanceFunction)raw : fallback;","tryCatchPattern":"try { builder.Property(d => d.Embedding).IsVectorProperty(df, dims); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"DistanceFunction\"))\n{ logger.InvalidDistanceFunction(df); /* fall back to a known good value */\n  builder.Property(d => d.Embedding).IsVectorProperty(DistanceFunction.Cosine, dims); }","preventionTips":["Bind distance function from configuration by name, not by int cast.","Wrap int->enum casts with Enum.IsDefined at the boundary.","After upgrading EF Core, diff the DistanceFunction members against your stored values.","Keep a single SafeDistance(...) helper and use it everywhere."],"tags":["ef-core","cosmos","validation","enum","vector-search"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}