none
IEntityDesignerExtendedProperty is causing the VSXmlDesignerBase to throw a NullReferenceException RRS feed

  • Question

  • I am trying to debug a problem with a Entity Model extension is provided additional properties and custom code generation (see http://efxaf.codeplex.com/workitem/32029)

    The problem is there seems to be some problems with some of the extended properties and for the life of me I cannot figure out why.

    When debugging the exception doesn't occur within the extension itself, the error is occuring within the MS libraries.

     

    A first chance exception of type 'System.NullReferenceException' occurred in Microsoft.Data.Tools.VSXmlDesignerBase.dll

    A first chance exception of type 'System.NullReferenceException' occurred in Microsoft.XmlEditor.dll

    A first chance exception of type 'System.NullReferenceException' occurred in Microsoft.Data.Tools.XmlDesignerBase.dll

    A first chance exception of type 'System.NullReferenceException' occurred in Microsoft.Data.Tools.XmlDesignerBase.dll

    A first chance exception of type 'System.NullReferenceException' occurred in Microsoft.Data.Entity.Design.EntityDesigner.dll

    A first chance exception of type 'System.NullReferenceException' occurred in Microsoft.Data.Tools.XmlDesignerBase.dll

    A first chance exception of type 'System.NullReferenceException' occurred in Microsoft.Data.Entity.Design.EntityDesigner.dll

    A first chance exception of type 'System.NullReferenceException' occurred in Microsoft.Data.Entity.Design.EntityDesigner.dll

    A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

    A first chance exception of type 'System.NullReferenceException' occurred in Microsoft.VisualStudio.Modeling.Sdk.10.0.dll

    A first chance exception of type 'System.NullReferenceException' occurred in Microsoft.VisualStudio.Modeling.Sdk.10.0.dll

    A first chance exception of type 'System.NullReferenceException' occurred in Microsoft.VisualStudio.Modeling.Sdk.Diagrams.10.0.dll

    So now I am trying to figure out why the Designer is getting a NullReference. I know in one of the extension properties that aren't working has code in there to determine if the Scalar type is of only 3 types and if not to not show the property in the designer, the property is here http://efxaf.codeplex.com/SourceControl/changeset/view/93870#1112490 and the factory here http://efxaf.codeplex.com/SourceControl/changeset/view/93870#1112481

    My question is: Is it valid to return null for the CreateProperty method of the IEntityDesignerExtendedProperty.CreateProperty? If not is there a way of making a property conditional?

    Are there any other ideas what may be causing the Designer to crash with NullReferenceExceptions?

    Look forward to your replies


    Thursday, January 5, 2012 3:16 AM

All replies

  • I have pinpointed the issue down to the code which when the Property is being retrieved, we have code to sets the Property and modifies the Document to include our Default value for the property.

    See http://efxaf.codeplex.com/SourceControl/changeset/view/93870#1112459 in the PropertyValue getter we have

                        if (!isValueFound)
                        {
                            if (propertyValue != null)
                            {
                                //force the value to be written to the XML file so that the code generation can read it
                                this.PropertyValue = propertyValue;
                            }
                        }

    then in the Setter we have

    T propertyValue;
                    XElement customElement;
                    XElement lastChild;
                    bool bValOutput;
    
                    propertyValue = value;
    
                    try
                    {
                        // Make changes to the .edmx document in an EntityDesignerChangeScope to enable undo/redo of changes.
                        using (EntityDesignerChangeScope scope = _context.CreateChangeScope(string.Format("Set {0} Property Value", this.PropertyName)))
                        {
                            customElement = null;
                            if (propertyValue != null)
                            {
                                if ((this.PropertyFormat.Contains("{value}") == false) &&
                                    (bool.TryParse(propertyValue.ToString(), out bValOutput) == true) &&
                                    (bValOutput == false))
                                {
                                    //Don't create the customElement as we don't really want to persist it for this property
                                    //as the PropertyFormat doesn't contain a {value} but the value is a boolean indicating
                                    //if the custom attribute should be created (in this case it's false so it shouldn't be generated)
                                }
                                else
                                {
                                    //Generate a "template" Xml Element using our Custom Property's ElementName and namespace and initialize
                                    //it with the PropertyValue being passed in.  Then add on the attributes (metadata) which determine
                                    //the Name, Type and Format string of data being persisted in the element
                                    //i.e. <BaseCustomAttribute-Size PropertyName="" PropertyType="" PropertyFormat="">propertyValue</BaseCustomAttribute-Size>
                                    customElement = new XElement(XName.Get(this.ElementName, _namespace), propertyValue);
                                    customElement.Add(new XAttribute(_xnPropertyName, this.PropertyName));
                                    customElement.Add(new XAttribute(_xnPropertyType, typeof(T).ToString()));
                                    customElement.Add(new XAttribute(_xnPropertyFormat, this.PropertyFormat));
                                }
                            }
    
                            if (_parent.HasElements)
                            {
                                //Determine if an Element matching our custom property already exists as a child of the
                                //current Entity.Property being processed in order to add/update/remove settings accordingly
                                lastChild = _parent.Elements().Where<XElement>(element => element != null &&
                                                                        element.Name == XName.Get(this.ElementName, _namespace) &&
                                                                        element.Attribute(_xnPropertyName).Value == this.PropertyName).LastOrDefault();
                                if (lastChild != null)
                                {
                                    // element already exists under the EntityType element, so update its value.
                                    if (propertyValue != null)
                                    {
                                        lastChild.SetValue(propertyValue);
                                    }
                                    else
                                    {
                                        //property value has been removed so let's remove the property
                                        lastChild.Remove();
                                    }
                                }
                                else
                                {
                                    if (customElement != null)
                                    {
                                        // element does not exist, so create a new one as the last child of the EntityType element.
                                        _parent.Elements().Last().AddAfterSelf(customElement);
                                    }
                                }
                            }
                            else
                            {
                                // The EntityType element has no child elements so create a new MyNewProperty element as its first child.
                                if (customElement != null)
                                {
                                    _parent.Add(customElement);
                                }
                            }
    
                            // Commit the changes.
                            scope.Complete();
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
    
                }

    The wierd thing is the code works fine and it does adjust the XML, however... I believe VS2010 then has some issues internally based on when we are "adjusting" the XML.

    I think it has to do when you are visualling modifying the model VS doesn't like us changing the document (ie, dragging new entities or associations on), the code works fine when you are setting the property values via the PropertyGrid.

    Is there a better way to handle DefaultValues?

    Thursday, January 5, 2012 10:39 PM
  • Hi,

    Have you resolved the issue? If not, you may require a more in-depth level of support.  Please visit the below link to see the various paid support options that are available to better meet your needs. http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone

    Thanks,

    Cathy Miller

    Monday, January 23, 2012 5:43 PM
    Moderator