RVN008 - Projected field not retrievable under the applied ProjectionBehavior
-
RVN008 reports fields in a static-index query projection that cannot be retrieved under the query's
ProjectionBehavior. -
Default severity: Info
Automated code fix: No -
In this article:
Description
Field retrieval:
When a query on a static index projects its results, RavenDB retrieves each projected field according to the query's ProjectionBehavior:
Default: from the index if the field is stored, otherwise from the source document.FromIndex,FromIndexOrThrow: only from stored index-fields.FromDocument,FromDocumentOrThrow: only from the source document.
For map-reduce indexes, projected fields come from the reduce results.
FromDocument and FromDocumentOrThrow are not supported; the query fails.
Unavailable fields:
An unavailable field is skipped with Default, FromIndex, or FromDocument.
With FromIndexOrThrow or FromDocumentOrThrow, the query throws instead.
What RVN008 checks:
RVN008 reports these fields at compile time by comparing each projected field with the index's stored fields and with the members of the source document type.
Trigger condition
When is RVN008 reported?
RVN008 checks projections on queries that target a static index, in sync and async sessions:
- By index class:
session.Query<T, TIndex>()
The index class can be generic and can be defined in the current project or a referenced project. - By index name:
session.Query<T>("IndexName")orsession.Query<T>(indexName: "IndexName"),
where the name is a string literal, matched to an index class as described in RVN007.
RVN008 also checks projections applied to a query stored in a local variable, following the query from the variable's declaration initializer. See Limitations for subsequent reassignments.
RVN008 reports projected fields that are not retrievable under the query's ProjectionBehavior,
in the following projection forms:
-
ProjectInto<TProjection>():
every public instance property and field ofTProjection, including inherited members.
Reported on the type argument. -
Selectwith an anonymous object or object initializer:
each member whose value is a member of the lambda parameter, such asx.Company.
Reported on that member. -
Query syntax:
the finalselectclause, checked the same way asSelect.
A field is retrievable:
-
From the index when it is stored:
registered withStore(…),Stores[…],StoresStrings[…],Stores.Add(…), orStoresStrings.Add(…)withFieldStorage.Yes. All index-fields count as stored when the index callsStoreAllFields(FieldStorage.Yes)or defines aReducefunction. -
From the source document when it is a public instance property or field of the document type the index maps, the
TDocumentofAbstractIndexCreationTask<TDocument>, even when the query uses another type, such as the index'sIndexEntryclass.
Projection behavior
RVN008 reads the behavior from .Customize(x => x.Projection(ProjectionBehavior.…)) calls that precede the projection.
If more than one such call is recognized, the last one before the projection applies.
When no such customization is found, Default is assumed.
When is RVN008 not reported?
RVN008 skips some fields and projection forms, and is also not reported when the index, its fields, or the projection behavior cannot be determined. RVN008 is not reported for the following fields and projections: RVN008 is also not reported when the index, its fields, or the behavior cannot be determined:Full list of cases not checked
Id in a Select projection or query-syntax select, which is always retrievable.
In ProjectInto, Id is checked like any other member.new OrderDto(x.Company)), single-member selects (Select(x => x.Company)),
and members whose value is computed or a nested path, such as x.Address.City.
a projection that follows Select, ProjectInto, SelectMany, GroupBy, OfType, Cast, or As in the chain.
any query expression containing an into continuation or ending with a group … by clause.
Projection(…) argument is not written as ProjectionBehavior.Member,
for example a variable or a fully qualified enum name.x => x.Company.
For example, Store(x => x.Address.City, FieldStorage.Yes) and Store(nameof(Order.Company), FieldStorage.Yes) stop analysis.StoreAllFields(…) argument cannot be recognized as FieldStorage.Yes or FieldStorage.No,
for example when it is supplied through a variable.
or matches more than one index class.IndexName with a value other than a string literal (string form only).CreateField, CreateSpatialField, or AsJson, which create field names at runtime.Map = GetMap() or a lambda with a block body,
or its final select, Select, or SelectMany projection is not an anonymous object or object initializer.CreateIndexDefinition().private, protected, or private protected, or nested in a class with one of these access levels.
How to resolve
Make each projected field retrievable under the query's behavior:
- Store the field in the index (
Default,FromIndex, andFromIndexOrThrow). - Project a field that exists on the source document (
Default,FromDocument, andFromDocumentOrThrow). - Otherwise, remove the field from the projection or change the
ProjectionBehavior.
Before (triggers RVN008)
public class Orders_WithTotal : AbstractIndexCreationTask<Order, Orders_WithTotal.IndexEntry>
{
public class IndexEntry
{
public string Company { get; set; }
}
public Orders_WithTotal()
{
Map = orders => from o in orders
select new IndexEntry
{
Company = o.Company
};
}
}
public class OrderDto
{
public string Company { get; set; }
// Total is not stored in the index and is not a property of Order.
public decimal Total { get; set; }
}
var results = session.Query<Orders_WithTotal.IndexEntry, Orders_WithTotal>()
// Triggers RVN008 on OrderDto: Total is not retrievable.
.ProjectInto<OrderDto>()
.Take(50)
.ToList();
After (correct)
public class Orders_WithTotal : AbstractIndexCreationTask<Order, Orders_WithTotal.IndexEntry>
{
public class IndexEntry
{
public string Company { get; set; }
public decimal Total { get; set; }
}
public Orders_WithTotal()
{
Map = orders => from o in orders
select new IndexEntry
{
Company = o.Company,
Total = o.Lines.Sum(l => l.Quantity * l.PricePerUnit * (1 - l.Discount))
};
// Store Total so the projection can retrieve it from the index.
Store(x => x.Total, FieldStorage.Yes);
}
}
var results = session.Query<Orders_WithTotal.IndexEntry, Orders_WithTotal>()
.ProjectInto<OrderDto>()
.Take(50)
.ToList();
Limitations
-
RVN008 reads index-fields and stored-field registrations only from constructor bodies of the index class and its base classes, not from helper methods they call.
Maps assigned entirely in a helper and fields stored only in a helper can be missed.RVN008 may therefore report a retrievable field under
FromIndexorFromIndexOrThrow, or underDefaultwhen the field is not a member of the source document. -
RVN008 follows local-variable declaration initializers but does not track subsequent reassignments. Later assignments can therefore make RVN008 check the original query or behavior instead of the one used at runtime.
-
RVN008 only reads expression-bodied
Customizelambdas whose final call isProjection(…), and only before the query projection. For example,x => x.Projection(…).WaitForNonStaleResults()is not recognized.
An ignored customization can cause RVN008 to assumeDefaultor use an earlier recognized behavior.
Related C# analyzers
- RVN007
Fires when a query filters, sorts, or searches on a field that the index's map function does not define.