{"record":{"id":"64f3fd0ddfaede5e","repo":"bchavez/Bogus","slug":"your-expression-expressionstring-cant-be-used","errorCode":null,"errorMessage":"Your expression '{expressionString}' cant be used. Nested accessors like 'o => o.NestedObject.Foo' at a parent level are not allowed. You should create a dedicated faker for NestedObject like new Faker<NestedObject>().RuleFor(o => o.Foo, ...) with its own rules that define how 'Foo' is generated. See this GitHub issue for more info: https://github.com/bchavez/Bogus/issues/115","messagePattern":"Your expression '(.+?)' cant be used\\. Nested accessors like 'o => o\\.NestedObject\\.Foo' at a parent level are not allowed\\. You should create a dedicated faker for NestedObject like new Faker<NestedObject>\\(\\)\\.RuleFor\\(o => o\\.Foo, \\.\\.\\.\\) with its own rules that define how 'Foo' is generated\\. See this GitHub issue for more info: https://github\\.com/bchavez/Bogus/issues/115","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Source/Bogus/PropertyName.cs","lineNumber":33,"sourceCode":"\n   public static string For<T>(Expression<Func<T, object>> expression)\n   {\n      Expression body = expression.Body;\n      return GetMemberName(body);\n   }\n\n   public static string For(Expression<Func<object>> expression)\n   {\n      Expression body = expression.Body;\n      return GetMemberName(body);\n   }\n\n   public static string GetMemberName(Expression expression)\n   {\n      var expressionString = expression.ToString();\n      if( expressionString.IndexOf('.') != expressionString.LastIndexOf('.') )\n      {\n         throw new ArgumentException(\n            $\"Your expression '{expressionString}' cant be used. Nested accessors like 'o => o.NestedObject.Foo' at \" +\n            $\"a parent level are not allowed. You should create a dedicated faker for \" +\n            $\"NestedObject like new Faker<NestedObject>().RuleFor(o => o.Foo, ...) with its own rules \" +\n            $\"that define how 'Foo' is generated. \" +\n            \"See this GitHub issue for more info: https://github.com/bchavez/Bogus/issues/115\");\n      }\n\n      MemberExpression memberExpression;\n\n      if( expression is UnaryExpression unary )\n         //In this case the return type of the property was not object,\n         //so .Net wrapped the expression inside of a unary Convert()\n         //expression that casts it to type object. In this case, the\n         //Operand of the Convert expression has the original expression.\n         memberExpression = unary.Operand as MemberExpression;\n      else\n         //when the property is of type object the body itself is the\n         //correct expression","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/bchavez/Bogus/blob/6ece18c5c22648ab4606e45a0aaf2f07fc5259e4/Source/Bogus/PropertyName.cs#L15-L51","documentation":"Thrown by PropertyName.GetMemberName when the expression string contains more than one '.' (IndexOf != LastIndexOf), indicating a nested accessor like o => o.NestedObject.Foo. Bogus only supports flat member access at the Faker<T> level; nested objects must have their own dedicated Faker.","triggerScenarios":"Calling RuleFor(x => x.Address.Street, ...) or FinishWith/PropertyName.For on a lambda that traverses into a sub-object.","commonSituations":"Modeling aggregates where a property is itself a complex type; assuming Bogus can auto-bind nested property paths like EF Core expressions.","solutions":["Create a separate Faker<NestedObject> with its own rules and use it in the parent's RuleFor to populate the nested property.","Flatten the model so the property is directly on T.","Use a plain setter lambda (f => new Nested { Foo = ... }) instead of an expression accessing into the nested object."],"exampleFix":"// before\nnew Faker<Order>().RuleFor(o => o.Customer.Name, f => f.Name.FullName());\n// after\nvar customerFaker = new Faker<Customer>().RuleFor(c => c.Name, f => f.Name.FullName());\nnew Faker<Order>().RuleFor(o => o.Customer, f => customerFaker.Generate());","handlingStrategy":"type-guard","validationCode":"static string SafeMember<T,P>(Expression<Func<T,P>> e) {\n    var s = e.Body.ToString();\n    if (s.IndexOf('.') != s.LastIndexOf('.')) throw new InvalidOperationException(\"nested accessor not allowed\");\n    return PropertyName.For(e as Expression<Func<object>>);\n}","typeGuard":"bool IsFlatAccessor(Expression e) {\n    var s = e.ToString();\n    return s.IndexOf('.') == s.LastIndexOf('.');\n}","tryCatchPattern":"try { f.RuleFor(expr, setter); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Nested accessors\")) {\n    // extract nested faker and rewrite RuleFor at parent level\n}","preventionTips":["Always model sub-objects with their own Faker<T>.","Lint expressions in a test: assert single-dot member access.","Review RuleFor lambdas during code review for any dot beyond x."],"tags":["bogus","propertyname","expression","nested-accessor"],"backgroundTag":null,"analyzedSha":"6ece18c5c22648ab4606e45a0aaf2f07fc5259e4","analyzedAt":"2026-08-13T21:08:15.463Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}