IEntityDesignerExtendedProperty.CreateProperty 方法

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

命名空间: Microsoft.Data.Entity.Design.Extensibility
程序集: Microsoft.Data.Entity.Design.Extensibility(在 microsoft.data.entity.design.extensibility.dll 中)

用法

语法

声明
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

参数

  • 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.

返回值

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

示例

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

备注

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.

线程安全

此类型的任何公共静态(在 Visual Basic 中为 共享)成员都是线程安全的。不保证所有实例成员都是线程安全的。

平台

开发平台

Windows XP Home Edition, Windows XP Professional, Windows Server 2003 、Windows Server 2008 和 Windows 2000

目标平台

Change History

另请参见

参考

IEntityDesignerExtendedProperty 接口
IEntityDesignerExtendedProperty 成员
Microsoft.Data.Entity.Design.Extensibility 命名空间

其他资源

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