Skip to content

Commit

Permalink
Handle null primary keys from virtual entity providers
Browse files Browse the repository at this point in the history
Fixes #511
  • Loading branch information
MarkMpn committed Jul 12, 2024
1 parent 79b4af3 commit 780259b
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions MarkMpn.Sql4Cds.Engine/ExecutionPlan/FetchXmlScan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -823,8 +823,19 @@ private void OnRetrievedEntity(Entity entity, INodeSchema schema, IQueryExecutio
sqlValue = SqlTypeConverter.NetToSqlType(dataSource, value, col.Value.Type);
}

if (_primaryKeyColumns.TryGetValue(col.Key, out var logicalName) && sqlValue is SqlGuid guid)
sqlValue = new SqlEntityReference(DataSource, logicalName, guid);
if (_primaryKeyColumns.TryGetValue(col.Key, out var logicalName))
{
if (sqlValue is SqlGuid guid)
sqlValue = new SqlEntityReference(DataSource, logicalName, guid);

if (_isVirtualEntity && sqlValue is SqlEntityReference er && er.IsNull && !col.Value.IsNullable)
{
// Virtual entity providers aren't reliable and can produce null guids for non-nullable primary keys
// https://github.com/MarkMpn/Sql4Cds/issues/511
// Make up a new guid to avoid the null value
sqlValue = new SqlEntityReference(DataSource, logicalName, Guid.NewGuid());
}
}

entity[col.Key] = sqlValue;
}
Expand Down

0 comments on commit 780259b

Please sign in to comment.