Gotchas

Overview

Things to look out for when writing native tools.

Queryable object creation must be identical

When using a queryable to get the data directly from the database, as opposed to materializing full entity objects and then pulling out the specific properties you want, you may run into the following error:

The type 'Rock.AI.Agent.Classes.Entity.PersonResult' appears in two structurally incompatible initializations within a single LINQ to Entities query. A type can be initialized in two places in the same query, but only if the same properties are set in both places and those properties are set in the same order.

This is a requirement of EF that all object initialization calls have the same structure. For example, lets take a look at this sample. We want to get both the requester and connector, but we want the connector to be able to display the photo.

queryable.Select( cr => new ConnectionRequestResult
{
    Requester = new PersonResult
    {
        NickName = cr.RequesterPersonAlias.Person.NickName,
        LastName = cr.RequesterPersonAlias.Person.LastName,
    },
    Connector = new PersonResult
    {
        NickName = cr.ConnectorPersonAlias.Person.NickName,
        LastName = cr.ConnectorPersonAlias.Person.LastName,
        PhotoId = cr.ConnectorPersonAlias.Person.PhotoId,
    }
} )

This will cause the above error because EF sees that we are creating two differently structured PersonResult objects. Even though this is logically perfectly valid, EF will have a problem with it. We don't know why, but you can't do this. Instead, you would have to make sure that all PersonResult objects are initialized the exact same way.