Print Page | Close Window

How to set a DefaultValue in enum properties in the Model?

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2012
Forum Discription: For .NET 4.5
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3867
Printed Date: 25-Oct-2025 at 1:23pm


Topic: How to set a DefaultValue in enum properties in the Model?
Posted By: cefernan
Subject: How to set a DefaultValue in enum properties in the Model?
Date Posted: 03-Jan-2013 at 8:29am
Hello,

I've converted some properties to enum in my model. It is working fine, but I need to set a DefaultValue for some these properties. 

I've set DomainModel.Enums.EnumMarkerType.Manual as DefaultValue of my property and I've got this error:
Running transformation: System.Exception: Unable to load MetadataWorkspace from file: D:\GIT\.Net\Project\Source\DomainModel\Model.edmx

This may be due to validation errors encountered during the load; Please check for EDMX validation errors.

The EntityContainer 'Entities' for the conceptual model specified as part of this MSL does not exist in MetadataWorkspace.
   at IdeaBlade.EntityModel.Edm.Metadata.EdmxWrapper.ThrowException(String edmxFileName, IEnumerable`1 errors)
   at IdeaBlade.EntityModel.Edm.Metadata.EdmxWrapper.LoadFrom(String edmxFileName, IEnumerable`1& errors)
   at IdeaBlade.VisualStudio.OM.CodeGenerator.BaseDomainModelTemplate.Generate()
   at Microsoft.VisualStudio.TextTemplating374DCA47E02B50469B3AF74A9D3CF78C2FE732F45BBB91A5A84F8207A9CECD2BFEE33DBD0E5DB5D9C1037716EAA87B6A1C7747F0C65E234E4248EC58DDD99C35.GeneratedTextTransformation.TransformText() in d:\GIT\.Net\Project\Source\DomainModel\Model.edmx.tt:line 11 d:\GIT\.Net\Project\Source\DomainModel\Model.edmx.tt 11 1 DomainModel (DomainModel\DomainModel)

Just to test, If do it directly in my generated class, it works fine:
    /// <summary>Gets or sets the Type. </summary>
    [DataMember]
    [Bindable(true, BindingDirection.TwoWay)]
    [Editable(true)]
    [Display(Name="Type", AutoGenerateField=true)]
    [IbVal.RequiredValueVerifier( ErrorMessageResourceName="PersonMarker_Type")]
    [DefaultValue(DomainModel.Enums.EnumMarkerType.Manual)]
    public DomainModel.Enums.EnumMarkerType Type {
      get { return PropertyMetadata.Type.GetValue(this); }
      set { PropertyMetadata.Type.SetValue(this, value); }
    }

Is it possible to set DefaultValue in EDM Designer (without an entity partial class)?



Replies:
Posted By: kimj
Date Posted: 03-Jan-2013 at 10:26am
EF itself doesn't support setting the DefaultValue on enum properties.  The error you're getting occurs because EF raises an error stating that "default values are allowed only for non-spatial primitive types", which in turn causes DevForce to fail when trying to load the EDMX.  
 
As you've found, the DefaultValue attribute works fine at run time.  So the workaround for now is to handle code gen yourself, use the http://drc.ideablade.com/devforce-2012/bin/view/Documentation/model-metadata-class - metadata buddy class approach, or use the EntityMetadata http://drc.ideablade.com/devforce-2012/bin/view/Documentation/model-examine - DefaultValueFunction .
 
Last week another customer ran into this issue too and we discussed opening a feature request on UserVoice for the problem, but neither of us has done so yet. 


Posted By: kimj
Date Posted: 03-Jan-2013 at 10:42am
I added this as an issue to the EF CodePlex site - http://entityframework.codeplex.com/workitem/762 - http://entityframework.codeplex.com/workitem/762 .


Posted By: cefernan
Date Posted: 03-Jan-2013 at 10:59am
Really nice Kim. Thanks for the post.

I'm avoiding to create metadata buddy classes. So, I'm trying to change code gen to solve this. 

Like this:
        protected override void WriteEntityDataPropertyAttributes(EdmPropertyWrapper property)
        {
            if ((property.IsEnumType) && (property.DefaultValue != null))
            {
                WriteAttribute("DefaultValue(" + property.DefaultValue.ToString() + ")");
                property.DefaultValue = null; -- hypothetical code, DefaultValue is read only. I need something like this to "clean" the DefaultValue and avoid the exception after, I guess
            }
            base.WriteEntityDataPropertyAttributes(property);
        }

Is it possible to intercept DefaultValue attribute like that?


Posted By: kimj
Date Posted: 03-Jan-2013 at 11:10am
I haven't tried this (yet), but I don't think this will work.  We're not able to load the EDMX if it contains a DefaultValue attribute on any property in the CSDL, so will never get to the point of generating code.  The DevForce "Tag" property would work though.  You can set the Tag via the designer, and no validation is performed. 


Posted By: cefernan
Date Posted: 03-Jan-2013 at 11:18am
Done!

Thanks again.



Print Page | Close Window