问题
Using the sample code at http://erikpool.blogspot.com/2011/03/filtering-generated-entities-with.html I have altered this so that GenerateEntity and GenerateOptionSet to have the code:
return optionSetMetadata.Name.ToLowerInvariant().StartsWith("myprefix");
This generates the types including some enumerations for the optionsets. The actual implementation of the optionset in the entity doesn't use this however, but I get the following:
[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("myprefix_fieldname")]
public Microsoft.Xrm.Sdk.OptionSetValue myprefix_FieldName
{
get
{
Microsoft.Xrm.Sdk.OptionSetValue optionSet = this.GetAttributeValue<Microsoft.Xrm.Sdk.OptionSetValue>("myprefix_fieldname");
if ((optionSet != null))
{
return ((Microsoft.Xrm.Sdk.OptionSetValue)(System.Enum.ToObject(typeof(Microsoft.Xrm.Sdk.OptionSetValue), optionSet.Value)));
}
else
{
return null;
}
}
set
{
this.OnPropertyChanging("myprefix_FieldName");
if ((value == null))
{
this.SetAttributeValue("myprefix_fieldname", null);
}
else
{
this.SetAttributeValue("myprefix_fieldname", new Microsoft.Xrm.Sdk.OptionSetValue(((int)(value))));
}
this.OnPropertyChanged("myprefix_FieldName");
}
}
Obviously casting the OptionSetValue to an int in the setter does not compile, I assume that it should be generating the property with a type that matches the generated enum, but isn't. What do I need to do to correct this?
回答1:
It looks like there was a bug in the crmsrvcutil that has since been fixed. My code for OptionSet properties now looks like this:
[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("prioritycode")]
public Microsoft.Xrm.Sdk.OptionSetValue PriorityCode
{
get
{
return this.GetAttributeValue<Microsoft.Xrm.Sdk.OptionSetValue>("prioritycode");
}
set
{
this.OnPropertyChanging("PriorityCode");
this.SetAttributeValue("prioritycode", value);
this.OnPropertyChanged("PriorityCode");
}
}
And I get no error setting the OptionSetValue...
来源:https://stackoverflow.com/questions/8443981/crm-2011-generated-code-using-icodewriterfilterservice-fails-to-build