locked
Category and DisplayName property attributes RRS feed

  • Question

  • Hi All,

    I am using VS2010 and EF4 for a new WinForms project I am working on. This is my first attempt at EF4 and come from many years of SubSonic.

    I am struggling to figure out how to set the Category and DisplayName attributes for the EF4 generated properties of my tables. I have come across many possible solutions and the only one that works is: http://social.msdn.microsoft.com/Forums/ko-KR/adodotnetentityframework/thread/617ebfca-0f68-4b90-83fd-0da758fadbd0
    This is however such a clunky solution and not neat and clean at all.

    I also found posts related to creating a Metadata class and hooking that to the partial class of the table names. This did not work for me at all.
    A simple sample of what I am trying to do...

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    
    namespace SL.PICData.EF
    {
     [MetadataType(typeof(Incident.IncidentMetaData))]
     public partial class Incident
     {
      internal sealed class IncidentMetaData
      {
       [DisplayName("Inc No")]
       public int IncidentsID { get; set; }
      }
     }
    }
    When I attach an instance of this class to a PropertyGrid I still get IncidentsID labeled as IncidentsID and not "Inc No" as I want.

    I am also going to need to add some custom editors to properties that display in the PropertyGrid, but if I cannot even get Category and DisplayName working I am fearing I won't get my custom editors working either.

    In SubSonic I altered the T4 templates so that all column properties were set in a base class that the SubSonic generated class inherited from, and made these properties virtual. Then I could create my own partial class, and override the properties in my partial class to set whatever attributes I liked. This worked great but the problem arose that when new versions of SubSonic were released I was in a bind as I had modified the T4 templates so could not simply update.
    Anyway, SubSonic is waaaaaay too slow for large datasets so looking at moving to EF4 for future use, but I really really need access to the generated properties - and easily.

    Can anyone point me in the right direction? Would be greatly appreciated. Nearly 2 days of fiddling with this and no resolve as yet (except for implementing ICustomTypeDescriptor which is not ideal at all).

    TIA - Mike
    Wednesday, November 10, 2010 5:03 PM

Answers

  • right click anywhere on the model and select add code generation item. You can choose poco template or default code generator whichever you prefer. This will give you the t4 template that is responsible for generating your classes. You can then modify the t4 template as you like to generate the right attributes.
    Zeeshan Hirani Entity Framework 4.0 Recipes by Apress
    http://weblogs.asp.net/zeeshanhirani
    • Marked as answer by Alex Liang Friday, November 12, 2010 2:32 AM
    Thursday, November 11, 2010 8:53 AM

All replies

  • Are you using POCO or self-tracking?

    Try to disable the proxy creation (ProxyCreationEnabled):

    http://msdn.microsoft.com/en-us/library/dd456853.aspx

     


    Please mark posts as answers/helpful if it answers your question
    Wednesday, November 10, 2010 9:54 PM
  • I am generating the classes in the designer. Still very new to EF4 so not familiar with the various terminologies yet.
    But definately not POCO. That must I can say :) The designer connects to my database and generates the classes from there.
    Thursday, November 11, 2010 8:08 AM
  • right click anywhere on the model and select add code generation item. You can choose poco template or default code generator whichever you prefer. This will give you the t4 template that is responsible for generating your classes. You can then modify the t4 template as you like to generate the right attributes.
    Zeeshan Hirani Entity Framework 4.0 Recipes by Apress
    http://weblogs.asp.net/zeeshanhirani
    • Marked as answer by Alex Liang Friday, November 12, 2010 2:32 AM
    Thursday, November 11, 2010 8:53 AM
  • Okay, I have just gone from a feeling of helplessness to one of power. I now have regained control over my code. Thanks so much for the tip Zeeshan!! Much appreciated.

    Edit: While I can now modify the T4 templates I am not satisfied. The main thing being that I am worried that later when EF is updated, I have to redo my modifications. Not good. I did however managed to pull the table fields into a base class and make each virtual, then the currently generated fields override (class inherits from new base of course) and carry on as normal. My own particial classes now have the ability to override the field properties and set attributes. All compiles. When executing my app, I now get MetaData errors. Hmmmm.... While this has opened doors for me again, I am not conformtable having to do all this just to simply set DisplayName and Category for my field properties. Is there no other simpler way to do this from my partial classes and just leave the designer generated stuff alone?
    • Edited by Michael Smit Thursday, November 11, 2010 3:19 PM Updated Response
    Thursday, November 11, 2010 2:13 PM