Hi,
I cannot get this to work. I am trying to do a Query and project a realated entity and project one of the related entitys related entitiy. Sounds more complex than it is. Here is my code example:
var query =
from file in context.Files
select new File
{
ID = file.ID,
CurrentReminder = file.CurrentReminder == null ? null : new Reminder
{
ID = file.CurrentReminder.ID,
Name = file.CurrentReminder.Name == null ? null : new Name
{
ID = file.CurrentReminder.Name.ID
}
}
};
The File type has a navigation property CurrentReminder of type Reminder, the property can be null for some File instances. The Reminder type has a Name property of type Name that can be null for some Reminder instances. So I need to have
null propagation checks to not break desirialization on the client so I have the == null ? null : construct in the expression.
The generated OData request is correct and the server returns some File entities with null for Reminder and some with content.
I get an exception when WCF DS Client is deserializing the response. As far as I can tell the exception occurs on the client when deserializing a File entity that has a CurrentReminder and the CurrentReminder has a Name. The exception thrown states:
"An entity of type 'Services.OData.Name' cannot be projected because there is already an instance of type 'Services.OData.Reminder' for 'http://localhost/Services.OData.Web/OData.svc/Reminders('21')'."
It seems as if WCF DS Client got confused of what it is doing and mixing up the types. The full stack trace is:
System.InvalidOperationException: An entity of type 'Services.OData.Name' cannot be projected because there is already an instance of type 'Services.OData.Reminder' for 'http://localhost/Services.OData.Web/OData.svc/Reminders('21')'.
at System.Data.Services.Client.Materialization.ODataEntityMaterializer.ProjectionInitializeEntity(ODataEntityMaterializer materializer, MaterializerEntry entry, Type expectedType, Type resultType, String[] properties, Func`4[] propertyValues)
at System.Data.Services.Client.Materialization.ODataEntityMaterializerInvoker.ProjectionInitializeEntity(Object materializer, Object entry, Type expectedType, Type resultType, String[] properties, Func`4[] propertyValues)
at _dynamic_ODataEntityMaterializerInvoker_ProjectionInitializeEntity(Object , Object , Type , Type , String[] , Func`4[] )
at lambda_method(Closure , Object , Object , Type )
at System.Data.Services.Client.Materialization.ODataEntityMaterializer.ProjectionInitializeEntity(ODataEntityMaterializer materializer, MaterializerEntry entry, Type expectedType, Type resultType, String[] properties, Func`4[] propertyValues)
at System.Data.Services.Client.Materialization.ODataEntityMaterializerInvoker.ProjectionInitializeEntity(Object materializer, Object entry, Type expectedType, Type resultType, String[] properties, Func`4[] propertyValues)
at _dynamic_ODataEntityMaterializerInvoker_ProjectionInitializeEntity(Object , Object , Type , Type , String[] , Func`4[] )
at lambda_method(Closure , Object , Object , Type )
at System.Data.Services.Client.Materialization.ODataEntityMaterializer.ProjectionInitializeEntity(ODataEntityMaterializer materializer, MaterializerEntry entry, Type expectedType, Type resultType, String[] properties, Func`4[] propertyValues)
at System.Data.Services.Client.Materialization.ODataEntityMaterializerInvoker.ProjectionInitializeEntity(Object materializer, Object entry, Type expectedType, Type resultType, String[] properties, Func`4[] propertyValues)
at _dynamic_ODataEntityMaterializerInvoker_ProjectionInitializeEntity(Object , Object , Type , Type , String[] , Func`4[] )
at lambda_method(Closure , Object , Object , Type )
at System.Data.Services.Client.ProjectionPlan.Run(ODataEntityMaterializer materializer, ODataEntry entry, Type expectedType)
at System.Data.Services.Client.Materialization.ODataEntityMaterializer.ReadImplementation()
at System.Data.Services.Client.MaterializeAtom.MoveNextInternal()
at System.Data.Services.Client.MaterializeAtom.MoveNext()
at System.Linq.Enumerable.<CastIterator>d__b1`1.MoveNext()
Is this a bug in WCF DS Client or should I write the null propagation checks diferent?
Please note that it Works without null propagation checks for entities that has related entities (but fails for nulls as expected).
Please also note that if write the Query untyped it also Works. The equivalent untyped Query is:
var query =
from file in context.Files.AddQueryOption("$expand", "CurrentReminder,CurrentReminder/Name").AddQueryOption("$select", "ID,CurrentReminder/ID,CurrentReminder/Name/ID")
select file;
Please help - this is driving me nuts.
Regards Uffe