Hi!
I generated early bound entities (specifically for the Account) and I wonder why the ParentAccountId EntityReference Attribute is no marked as optional / nullable? As far as I can see only bool, int, long, enum and Guid attributes are marked as nullable. But still, an EntityReference can be null, right? Why is this not marked in the generated classes?
Solved! Go to Solution.
or you can submit an issue to Early Bound Generator for XrmToolBox https://github.com/daryllabar/DLaB.Xrm.XrmToolBoxTools/wiki/Early-Bound-Generator describing this requirement and check if it will be implemented in a next release.
EDIT: never mind, I saw that you probably created an issue some hours ago, make sure to include the same link you added here also there
Classes are nullable by default, so if the ParentAccountId has been mapped to a class you can assign null to the property. the other types you mentioned need to be explicitly set as nullable (with a syntax like bool?) otherwise they are not nullable by default.
I don't know if this answers your question but feel free to put some screenshots to explain how your classes are generated.
I somehow thought that since bool fields are marked as nullable, every other fields can also be marked as nullable. If the type of a lookup field would be "EntityReference?", Visual Studio would know that it can be null and warn me if I would try to use it before checking if it is null. bool? fields are marked as nullable, which makes Visual Studio warn me if I don't check for null. It would be convenient if the class generator would also mark every other field as nullable.
EntityReference is a class, you can't define something like
EntityReference? myVariable
because you can assign it null directly:
EntityReference myVariable = null;
bool, int and other primitive types can't be set to null, for example
bool myVariable = null;
will return an error, to make the variable to accept null (because Dynamics CRM / Dataverse can store null inside these attributes too) the syntax is bool?
Keep in mind that you can cast automatically a field to a non-nullable value, like if you use late bound you can get the boolean value to a bool variable and if is null it sets to the default value (false in this example)
Here a post I wrote that mention this behavior: https://www.crmanswers.net/2015/04/getattributevalue-demystified.html
You get that warnings because you are using a recent version of Visual Studio (like 2022 or 2019), inside Visual Studio 2017 you don't get that kind of warnings.
I appreciate your reply. I am using C# 8.0, which has nullable reference types: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-reference-t...
This enables me to write practically "null-safe" code because classes can be marked as null too. The C# compiler will check if a reference can be null and warn me if I haven't checked if it is null. It's a great feature which can also be found in TypeScript or Kotlin.
I guess the early bound entity generator doesn't have C# 8.0 in mind yet, because all attributes should be marked as nullable but aren't. As you described in your blog post, all non-primitive attribute types (like EntityReferences) can be null and will return null if no value is set in the CRM. It would be great to have all attributes marked as nullable.
I could modify the generated files to be nullable but this doesn't seem to be a good idea. Maybe by extending the early bound generator https://docs.microsoft.com/en-us/powerapps/developer/data-platform/org-service/extend-code-generatio... you can force it to make all attributes nullable. Another solution would be to write a script to modify all generated files.
or you can submit an issue to Early Bound Generator for XrmToolBox https://github.com/daryllabar/DLaB.Xrm.XrmToolBoxTools/wiki/Early-Bound-Generator describing this requirement and check if it will be implemented in a next release.
EDIT: never mind, I saw that you probably created an issue some hours ago, make sure to include the same link you added here also there
User | Count |
---|---|
1 | |
1 | |
1 | |
1 | |
1 |
User | Count |
---|---|
5 | |
2 | |
2 | |
2 | |
2 |