New Posts New Posts RSS Feed: How to set a DefaultValue in enum properties in the Model?
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

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

 Post Reply Post Reply
Author
cefernan View Drop Down
Groupie
Groupie


Joined: 13-Jul-2012
Posts: 70
Post Options Post Options   Quote cefernan Quote  Post ReplyReply Direct Link To This Post Topic: How to set a DefaultValue in enum properties in the Model?
    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)?


Edited by cefernan - 03-Jan-2013 at 9:02am
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post 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 metadata buddy class approach, or use the EntityMetadata 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. 
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Posted: 03-Jan-2013 at 10:42am
I added this as an issue to the EF CodePlex site - http://entityframework.codeplex.com/workitem/762.
Back to Top
cefernan View Drop Down
Groupie
Groupie


Joined: 13-Jul-2012
Posts: 70
Post Options Post Options   Quote cefernan Quote  Post ReplyReply Direct Link To This Post 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?


Edited by cefernan - 03-Jan-2013 at 11:00am
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post 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. 
Back to Top
cefernan View Drop Down
Groupie
Groupie


Joined: 13-Jul-2012
Posts: 70
Post Options Post Options   Quote cefernan Quote  Post ReplyReply Direct Link To This Post Posted: 03-Jan-2013 at 11:18am
Done!

Thanks again.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down