Partager via


Méthode IEntityDesignerExtendedProperty.CreateProperty

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

Espace de noms: Microsoft.Data.Entity.Design.Extensibility
Assembly : Microsoft.Data.Entity.Design.Extensibility (dans microsoft.data.entity.design.extensibility.dll)

Utilisation

Syntaxe

'Déclaration
Function CreateProperty ( _
    xElement As XElement, _
    context As PropertyExtensionContext _
) As Object
Object CreateProperty (
    XElement xElement,
    PropertyExtensionContext context
)
Object^ CreateProperty (
    XElement^ xElement, 
    PropertyExtensionContext^ context
)
Object CreateProperty (
    XElement xElement, 
    PropertyExtensionContext context
)
function CreateProperty (
    xElement : XElement, 
    context : PropertyExtensionContext
) : Object

Paramètres

  • xElement
    The element in the .edmx file that defines the object that is selected in the 4ccd7ad6-b934-4f7c-82a0-cfd2d4a95faf Entity Data Model Designer or the 94e836e8-a5ea-47ff-aa3e-599d8a02ebfd Model Browser
  • context
    Provides file and Visual Studio project information.

Valeur de retour

An object whose public properties are displayed in the Visual Studio Properties window. For more information, see PropertyGrid.

Exemple

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 Studio Properties 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();
            }
        }
    }
}

Notes

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 Studio Properties 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.

Sécurité des threads

Tous les membres publics statiques (Partagés dans Visual Basic) de ce type sont thread-safe. Il n'est pas garanti que les membres d'instance soient thread-safe.

Plateformes

Plateformes de développement

Windows XP Édition familiale, Windows XP Professionnel, Windows Server 2003 , Windows Server 2008 et Windows 2000

Plateformes cibles

Change History

Voir aussi

Référence

Interface IEntityDesignerExtendedProperty
Membres IEntityDesignerExtendedProperty
Espace de noms Microsoft.Data.Entity.Design.Extensibility

Autres ressources

.edmx File Overview
Visual Studio Extensibility Developer Center
Developing Visual Studio Extensions