I just used PowerApps cli to generate (c#) Dataverse entities and one thing I don't understand is when an entity references another, the generated classes will have a property of the "EntityReference" type which will only have the name (field marked as display name), id and little more.
In a query expression where I link the two entities and then execute "RetrieveMultiple" I will never be able to get the fields of the linked entity. I either need to:
put an alias and column set in the link and then "swim" in the main entity attributes with "entity["."]
var query = new QueryExpression()
{
EntityName = Incident.EntityLogicalName,
ColumnSet = new ColumnSet("casetypecode", "contactid", "createdon", "incidentid",
"modifiedon", "prioritycode", "statuscode", "title")
};
var queryMyEntityLink = query.AddLink(myEntity.EntityLogicalName, "myentityid", "myentityid", JoinOperator.LeftOuter);
queryCountryChargerLink.Columns.AddColumns("field1","field2");
queryCountryChargerLink.EntityAlias = "myEntity";
Later in the code, to retrieve values I do
var dto = new IncidentDto()
{
Title = incidentEntity.Title,
...
};
if(incidentEntity.MyEntity != null)
{
dto.MyEntity = new MyEntity()
{
Field1 = (incidentEntity["myEntity.field1"] as AliasedValue)?.Value as string,
Field2 = (incidentEntity["myEntity.field2"] as AliasedValue)?.Value as string
};
}
Are these really the only two ways?
To translate the result of "RetrieveMultiple" into an entity class I do:
_service.RetrieveMultiple(query)?.Entities?.Select(entity => entity.ToEntity<T>())
Note: I'm using "Micrsoft.PowerPlatform.Dataverse.Client" to access the dataverse and .net core 6.
Pretty much.
There's a way to retrieve related records by specifying relationships using a RetrieveRequest but that only works if you're query a single particular parent record that you know the id of. See: https://dyncrmexp.com/2017/10/24/retrieve-primary-entities-along-with-related-entities-with-one-queryexpression/
When you're querying an arbitrary number of "parent" records with RetrieveMultiple, your method #1 (linked aliases) or #2 (separate queries) are the only options.
It sorts of make sense. If you could request related entities of a RetrieveMultiple, the engine might not be able to do better than generate an extra query per row that can easily lead to performance issues (the N+1 query problem). It's often more efficient to do a single second query filtered by the ids of the first (WHERE IN) or to link (JOIN) them in the first place.
The main annoyance with LinkEntities is accessing AliasedValues, but it is possible to write extensions methods to make it easier to unwrap values or extract Entities. See: https://github.com/daryllabar/DLaB.Xrm/blob/master/DLaB.Xrm.Core/Extensions/AliasedExtensions.cs
Alternately, using the LINQ provider instead of QueryExpression can make it easier to access related columns, although this method doesn't support all queries possible with QueryExpression. See: https://learn.microsoft.com/en-us/power-apps/developer/data-platform/org-service/linq-query-examples
To answer the title question, the EntityReference id represents the value of the Foreign Key column as stored in the SQL database table (with names in bonus) that enables you to query the related entity as needed.