Add empty check on ToEntityReference method
Add this Guid.Empty check and return null if empty.
Reason:
EntityReference account = new Account();
This should assign null but instead it assigns an EntityReference with Guid.Empty. This throws an error when trying to Save.
The error can be resolved if it assigned null as EntityReference instead.
public EntityReference ToEntityReference()
{
if (Id == Guid.Empty) return null;
return new EntityReference(LogicalName, Id)
{
RowVersion = RowVersion
};
}
-
Troy Spjute commented
While you are making changes to the ToEntityReference in the BaseProxyBlass, I would like the following code to be implemented to also add the Name of the entity reference. It would also require that all generated types specifically override the PrimaryNameAttribute (but they basically do already)
```
public virtual string PrimaryNameAttribute { get; }//
// Summary:
// Creates a reference for the current entity instance.
public new EntityReference ToEntityReference()
{
if (Id == Guid.Empty) return null;
var eRef = new EntityReference(LogicalName, Id)
{
RowVersion = RowVersion
};if (!string.IsNullOrEmpty(PrimaryNameAttribute) && this.Contains(PrimaryNameAttribute))
{
eRef.Name = this.GetAttributeValue<string>(PrimaryNameAttribute);
}return eRef;
}
```