IEntityDesignerExtendedProperty.CreateProperty Method

Creates a new property for an object that is selected in the Entity Data Model Designer or the Model Browser.

Namespace:  Microsoft.Data.Entity.Design.Extensibility
Assembly:  Microsoft.Data.Entity.Design.Extensibility (in Microsoft.Data.Entity.Design.Extensibility.dll)

Syntax

'Declaration
Function CreateProperty ( _
    xElement As XElement, _
    context As PropertyExtensionContext _
) As Object
'Usage
Dim instance As IEntityDesignerExtendedProperty
Dim xElement As XElement
Dim context As PropertyExtensionContext
Dim returnValue As Object

returnValue = instance.CreateProperty(xElement, _
    context)
Object CreateProperty(
    XElement xElement,
    PropertyExtensionContext context
)
Object^ CreateProperty(
    XElement^ xElement, 
    PropertyExtensionContext^ context
)
function CreateProperty(
    xElement : XElement, 
    context : PropertyExtensionContext
) : Object

Parameters

Return Value

Type: System.Object
An object whose public properties are displayed in the Visual StudioProperties window. For more information, see PropertyGrid.

Remarks

In a Visual Studio extension, the CreateProperty method is used to extend the functionality of the Entity Data Model Designer (Entity Designer). Specifically, the CreateProperty method adds a property to the Visual StudioProperties window when a specified object is selected in the Entity Designer or the Model Browser. The selected objects that cause the CreateProperty method to be called are specified by using the EntityDesignerExtendedPropertyAttribute.

For more information about extending the functionality of the ADO.NET Entity Data Model Tools, see Extending the Entity Data Model Tools and ADO.NET Entity Data Model Designer Extension Starter Kit.

Examples

In the example below, the MyNewProperty2Factory class implements the IEntityDesignerExtendedProperty interface. Note that an EntityDesignerExtendedPropertyAttribute is applied to the class to indicate that the CreateProperty method is called when a conceptual model entity type is selected in the Entity Designer or the Model Browser. The public properties of MyNewProperty2 (also shown below) are shown in the Visual StudioProperties window.

[PartCreationPolicy(CreationPolicy.Shared)]
[Export(typeof(IEntityDesignerExtendedProperty))]
[EntityDesignerExtendedProperty(EntityDesignerSelection.ConceptualModelEntityType)]
class MyNewProperty2Factory : IEntityDesignerExtendedProperty
{
    /// <summary>
    /// Called when the selected object in the Entity Data Model Designer changes and 
    /// matches the specified EntityDesignerExtendedProperty attribute.
    /// </summary>
    /// <param name="element"></param>
    /// <param name="context"></param>
    /// <returns></returns>
    public object CreateProperty(XElement element, PropertyExtensionContext context)
    {
        return new MyNewProperty2(element, context);
    }
}

The following code defines the MyNewProperty2 class that is referenced in the code above.

/// <summary>
/// This class has one public property, MyIntProperty. This property is visible in the Visual Studio
/// Properties window when a conceptual model entity type is selected in the Entity Designer or in 
/// the Model Browser.
/// This property and its value are saved as a structured annotation in an EntityType element in the
/// conceptual content of an .edmx file.
/// </summary>
class MyNewProperty2
{
    internal static readonly string _namespace = "http://schemas.tempuri.com/MyNewProperty2";
    internal static XName _xnMyNamespace = XName.Get("MyNewProperty2", _namespace);
    internal const string _category = "Example Property 2";

    private XElement _parent;
    private PropertyExtensionContext _context;

    public MyNewProperty2(XElement parent, PropertyExtensionContext context)
    {
        _context = context;
        _parent = parent;
    }

    // This property is saved in the conceptual content of an .edmx file in the following format:
    // <EntityType>
    //  <!-- other entity properties -->
    //  <MyNewProperty2 xmlns:a="http://schemas.tempuri.com/MyNewPropertyw">0</MyNewProperty>
    // </EntityType>
    [DisplayName("My New Property 2")]
    [Description("This property is available when a conceptual model entity type is selected" +
        " in the Entity Designer or the Model Browser. The property and its values are saved" + 
        " as a structured annotation in the EntityType element in the .edmx file.")]
    [Category(MyNewProperty2._category)]
    [DefaultValue(0)]
    public int MyIntProperty
    {
        // Read and return the property value from the structured anotation in the EntityType element.
        get
        {
            int propertyValue = 0;
            if (_parent.HasElements)
            {
                XElement lastChild = _parent.Elements().Last();
                if (lastChild.Name == MyNewProperty2._xnMyNamespace)
                {
                    // MyNewProperty2 element exists, so get its value.
                    int intValue = 0;
                    if (Int32.TryParse(lastChild.Value.Trim(), out intValue))
                    {
                        propertyValue = intValue;
                    }
                }
            }
            return propertyValue;
        }

        // Write the new property value to the structured anotation in the EntityType element.
        set
        {
            int propertyValue = value;

            // Make changes to the .edmx file in an EntityDesignerChangeScope to enable undo/redo of changes.
            using (EntityDesignerChangeScope scope = _context.CreateChangeScope("Set MyNewProperty2"))
            {
                if (_parent.HasElements)
                {
                    XElement lastChild = _parent.Elements().Last();
                    if (lastChild.Name == MyNewProperty2._xnMyNamespace)
                    {
                        // MyNewProperty2 element already exists under the EntityType element, 
                        // so update its value.
                        lastChild.SetValue(propertyValue.ToString());
                    }
                    else
                    {
                        // MyNewProperty2 element does not exist, so create a new one as the last child 
                        // of the EntityType element.
                        lastChild.AddAfterSelf(new XElement(_xnMyNamespace, propertyValue.ToString()));
                    }
                }
                else
                {
                    // MyNewProperty2 element does not exist, so create a new one as the last child of
                    // the EntityType element.
                    _parent.Add(new XElement(_xnMyNamespace, propertyValue.ToString()));
                }

                // Commit the changes.
                scope.Complete();
            }
        }
    }
}

Permissions

See Also

Reference

IEntityDesignerExtendedProperty Interface

IEntityDesignerExtendedProperty Members

Microsoft.Data.Entity.Design.Extensibility Namespace

Other Resources

.edmx File Overview (Entity Framework)

Visual Studio Extensibility Developer Center

Developing Visual Studio Extensions