bchavez/Bogus · error · ArgumentException
Your expression '{expressionString}' cant be used. Nested ac
Error message
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 What it means
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.
Source
Thrown at Source/Bogus/PropertyName.cs:33
public static string For<T>(Expression<Func<T, object>> expression)
{
Expression body = expression.Body;
return GetMemberName(body);
}
public static string For(Expression<Func<object>> expression)
{
Expression body = expression.Body;
return GetMemberName(body);
}
public static string GetMemberName(Expression expression)
{
var expressionString = expression.ToString();
if( expressionString.IndexOf('.') != expressionString.LastIndexOf('.') )
{
throw new ArgumentException(
$"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");
}
MemberExpression memberExpression;
if( expression is UnaryExpression unary )
//In this case the return type of the property was not object,
//so .Net wrapped the expression inside of a unary Convert()
//expression that casts it to type object. In this case, the
//Operand of the Convert expression has the original expression.
memberExpression = unary.Operand as MemberExpression;
else
//when the property is of type object the body itself is the
//correct expressionView on GitHub (pinned to 6ece18c5c2)
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.
Example fix
// before new Faker<Order>().RuleFor(o => o.Customer.Name, f => f.Name.FullName()); // after var customerFaker = new Faker<Customer>().RuleFor(c => c.Name, f => f.Name.FullName()); new Faker<Order>().RuleFor(o => o.Customer, f => customerFaker.Generate());
Defensive patterns
Strategy: type-guard
Validate before calling
static string SafeMember<T,P>(Expression<Func<T,P>> e) {
var s = e.Body.ToString();
if (s.IndexOf('.') != s.LastIndexOf('.')) throw new InvalidOperationException("nested accessor not allowed");
return PropertyName.For(e as Expression<Func<object>>);
} Type guard
bool IsFlatAccessor(Expression e) {
var s = e.ToString();
return s.IndexOf('.') == s.LastIndexOf('.');
} Try / catch
try { f.RuleFor(expr, setter); }
catch (ArgumentException ex) when (ex.Message.Contains("Nested accessors")) {
// extract nested faker and rewrite RuleFor at parent level
} Prevention
- 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.
When it happens
Trigger: Calling RuleFor(x => x.Address.Street, ...) or FinishWith/PropertyName.For on a lambda that traverses into a sub-object.
Common situations: Modeling aggregates where a property is itself a complex type; assuming Bogus can auto-bind nested property paths like EF Core expressions.
Related errors
- Expression was not of the form 'x => x.Property or x => x.Fi
- {nameof(TType)} must be the same type as parameter named '{n
- Cannot create a rule set within a rule set.
- alphabet must contain at least 4 unique characters.
- A premium license is required to use this API. Please double
AI-assisted analysis of bchavez/Bogus@6ece18c5c2 (2026-08-13).
Data as JSON: /api/errors/64f3fd0ddfaede5e.
Report an issue: GitHub.