How to automatically set default value for custom property in EDMX Designer Extension?

Unanswered How to automatically set default value for custom property in EDMX Designer Extension?

  • Monday, March 18, 2013 8:29 AM
     
      Has Code

    I created EDM Designer Extension with custom property BaseDiagram. I would like to automatically set BaseDiagram property when adding new entity to diagram. Default BaseDiagram value should be set according to name of diagram where is new entity added. I can set default value in getter, but this value is still the same for all Entities.

    using Microsoft.Data.Entity.Design.Extensibility;
    using System.ComponentModel;
    using System.ComponentModel.Composition;
    using System.Linq;
    using System.Xml.Linq;
    
    namespace MyNamespace.EFExtPackage
    {
      [PartCreationPolicy(CreationPolicy.Shared)]
      [Export(typeof(IEntityDesignerExtendedProperty))]
      [EntityDesignerExtendedProperty(EntityDesignerSelection.ConceptualModelEntityType)]
      public class EFDesignerCustomPropertiesFactory : IEntityDesignerExtendedProperty
      {
        public object CreateProperty(XElement element, PropertyExtensionContext context)
        {
          return new EFDesignerCustomProperties(element, context);
        }
      }
    
      public class EFDesignerCustomProperties
      {
        internal static readonly string _namespace = "http://myaddress.cz/";
        internal static XName _ns_BaseDiagram = XName.Get("BaseDiagram", _namespace);
        internal const string _category = "Custom";
    
        private XElement _parent;
        private PropertyExtensionContext _context;
    
        public EFDesignerCustomProperties(XElement parent, PropertyExtensionContext context)
        {
          _context = context;
          _parent = parent;
        }
    
        [DisplayName("Base Diagram")]
        [Description("Specifies base diagram for current table")]
        [Category(EFDesignerCustomProperties._category)]
        [DefaultValue(BaseDiagram.Common)]
        public BaseDiagram CustomBaseDiagram
        {
          get
          {
            BaseDiagram propertyValue = BaseDiagram.Common;
            if (_parent.HasElements)
            {
              foreach (XElement element in _parent.Elements())
              {
                if (element.Name == EFDesignerCustomProperties._ns_BaseDiagram)
                {
                  string bv = element.Value.Trim();
    
                  switch (bv)
                  {
                    case "Accounting": propertyValue = BaseDiagram.Accounting; break;
                    case "Bussiness": propertyValue = BaseDiagram.Bussiness; break;
                    case "Clients": propertyValue = BaseDiagram.Clients; break;
                    case "Common": propertyValue = BaseDiagram.Common; break;
                    case "Finance": propertyValue = BaseDiagram.Finance; break;
                    case "Products": propertyValue = BaseDiagram.Products; break;
                    case "Service": propertyValue = BaseDiagram.Service; break;
                    case "Stock": propertyValue = BaseDiagram.Stock; break;
                  }
                }
              }
            }
            return propertyValue;
          }
    
          set
          {
            BaseDiagram propertyValue = value;
            using (EntityDesignerChangeScope scope = _context.CreateChangeScope("Set Base Diagram"))
            {
              if (_parent.HasElements)
              {
                bool updated = false;
                foreach (XElement element in _parent.Elements())
                {
                  if (element.Name == EFDesignerCustomProperties._ns_BaseDiagram)
                  {
                    element.SetValue(propertyValue.ToString().Trim());
                    updated = true;
                    break;
                  }
                }
    
                if (!updated)
                {
                  XElement lastChild = _parent.Elements().Last();
                  lastChild.AddAfterSelf(new XElement(_ns_BaseDiagram, propertyValue));
                }
              }
              else
              {
                _parent.Add(new XElement(_ns_BaseDiagram, propertyValue.ToString().Trim()));
              }
    
              scope.Complete();
            }
          }
        }
    
        public enum BaseDiagram
        {
          Accounting,
          Bussiness,
          Clients,
          Common,
          Finance,
          Products,
          Service,
          Stock
        }
      }
    }


    Pavel Bily

All Replies

  • Tuesday, March 19, 2013 9:03 AM
    Moderator
     
     

    Hi Pavel,

    Welcome to the MSDN forum.

    I am trying to involve a senior expert into your thread. Please wait for the response. Sorry for any inconvenience.

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • Friday, March 29, 2013 10:17 AM
     
     

    Hello,

    I´m still waiting for response. Thanks for response.


    Pavel Bily

  • Thursday, April 11, 2013 9:02 PM
     
     

    Hi Pavel,

    Is it possible for you to create a small application which reproduces what you are seeing? This will help us have something which we can have a look at, at our end.

    Thanks,

    Kunal (MSFT)

  • Monday, April 15, 2013 6:37 PM
     
     
    I created sample application demonstrating my problem. It´s HERE. First I create New Entity type (table). Then open diagram and click on "Add to diagram". I would like to automatically set my property "Base diagram" according to name of diagram where I first add entity type. 

    Example: When I add Entity Type to diagram Stock, I would like to have "Base diagram" property automatically set on Stock.

    Pavel Bily



    • Edited by TimCzech Monday, April 15, 2013 6:41 PM
    •  
  • Friday, May 10, 2013 7:02 PM
     
     

    Hi Pavel,

    There is no way currently to get the diagram the entity belongs to with the public API. I would suggest you to report this issue on the http://entityframework.codeplex.com/ site. Doing this you can provide suggestions to include this in the next release.

    Thanks,

    Kunal (MSFT)