locked
Display column names in user friendly format RRS feed

  • Question

  • User4068249 posted

    I'm using VS2010, C# and the Entity Framework.  I created an Asp.net Dynamic Data solution and then created an EF model from selected tables in my SQL Server database.

    The app works but the column names are displayed exactly as they are named (camel case). Is there a way to apply a DataAnnotation to certain columns to make the column names always appear in a more readable format (ie: Item Name instead of ItemName)?

    I would also like to do the same for the Entities/Tables themselves. Is that possible?

     

    Thursday, September 5, 2013 2:09 PM

Answers

  • User4068249 posted

    I created a new project and was very careful about naming my solution and data model namespaces consistently.

    It worked like a charm.

    Thanks for the help!

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 12, 2013 9:56 AM

All replies

  • User1564875471 posted

    If you want to change the column/table names in the UI and also in the classes, I think the best way is to open the generated EF model and re-name the columns/entityes as you want. By doing this, the classes and also the dyanmic forms will be using the new names.

    Thursday, September 5, 2013 3:34 PM
  • User4068249 posted

    I don't see how that could work. I want to describe these things with multiple words separated by spaces (ie: Item Name).

    I don't think that's practical (or even possible?) for class or property names....

     

    Thursday, September 5, 2013 3:41 PM
  • User1564875471 posted

    In this case, you will have to create partial class + meta data class for each table/entity and provider new dispaly names for each property through the [Dispaly] attribute. Here is an example in this thread:

    http://forums.asp.net/t/1805409.aspx

    Thursday, September 5, 2013 3:50 PM
  • User4068249 posted

    I've got 2 issues. I did that for one column as a test. My code is below. It doesn't work. The heading still shows as DiscountDescription on all of the web forms that I've seen (I haven't changed any templates).

    In addition, I get a compile-time error if I apply the Display data-annotation to my partial class.

    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;

    namespace POSSetupDD
    {
        [ScaffoldTable(true)]
        [MetadataType(typeof(DiscountMetadata))]
        //[Display(Name = "Discounts")]
        public partial class PSDiscount
        {
        }

        public class DiscountMetadata
        {
            [Display(Name = "Description")]
            public object DiscountDescription { get; set; }
        }
    }

    Thursday, September 5, 2013 4:13 PM
  • User1564875471 posted

    I've got 2 issues. I did that for one column as a test. My code is below. It doesn't work. The heading still shows as DiscountDescription on all of the web forms that I've seen (I haven't changed any templates).

    In order for this to work, the namespace for your new classes (PoSSetupDD in your case) should be the same as the namespace for the related EF class , which is the one that will be generated in the EF. 

    As for customizing the dispaly for the entity itself, you should use the "DispalyName" attribute.

    Thursday, September 5, 2013 4:38 PM
  • User4068249 posted

    Yes, it is the same namespace.  The snippet below is from the edmx file.

    Also, I added the DisplayName annotation to the entity and it made no difference to the UI. No error but no change in headings/labels.

    namespace POSSetupDD
    {
     ...
        /// </summary>
        [EdmEntityTypeAttribute(NamespaceName="POS2005Model", Name="PSDiscount")]
        [Serializable()]
        [DataContractAttribute(IsReference=true)]
        public partial class PSDiscount : EntityObject
        {
            public static PSDiscount CreatePSDiscount(global::System.Int32 pSDiscountId, global::System.Int32 pSLocationId,
     global::System.String discountDescription, global::System.Int16 appliesTo, global::System.Decimal amountPercent,
     global::System.Int16 discountType, global::System.Boolean useMgrPassword)
            {
                PSDiscount pSDiscount = new PSDiscount();
                pSDiscount.PSDiscountId = pSDiscountId;
                pSDiscount.PSLocationId = pSLocationId;
                pSDiscount.DiscountDescription = discountDescription;
                pSDiscount.AppliesTo = appliesTo;
                pSDiscount.AmountPercent = amountPercent;
                pSDiscount.DiscountType = discountType;
                pSDiscount.UseMgrPassword = useMgrPassword;
                return pSDiscount;
            }
      ...
     
    }

    Thursday, September 5, 2013 4:53 PM
  • User1564875471 posted

    Two more things to check:

    1. Make sure to place the partial classes in the same project that contains your EF model.
    2. try to place the meta data class inside the partial class as below:
    namespace POSSetupDD
    {
        [ScaffoldTable(true)]
        [MetadataType(typeof(DiscountMetadata))]
        [DisplayName(Name = "Discounts")]
        public partial class PSDiscount
        {
          public class DiscountMetadata
          {
            [Display(Name = "Description")]
            public object DiscountDescription { get; set; }
          }
        }
    }

    3-Make sure that the type for the DiscountDescription in the meta data matched with the actual field type.

    4-If you are working with .NET 3.5, try DispalyName instead of dispaly attribute for the DiscountDescription.

    Thursday, September 5, 2013 5:03 PM
  • User4068249 posted

    1. There is only one project in this solution.

    2. Placed the metadata class inside the partial class, no difference.

    3. The examples I've seen define the column properties in the custom partial class as object. I changed it to string to be consistent, no difference.

    4. I'm using Framework 4.

    What else could I be missing?

     

    Friday, September 6, 2013 1:11 PM
  • User-227760790 posted

    hi db_csg,

    if you use html data  control, i suggest you directly change headings/labels. e.g. GridView HeaderText property.

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

    if you want to change  Entity Model class,you'd better to re-name the columns/entityes as you want in your SQL Server . and then generated EF model .

    Hope it helps u.

    Tuesday, September 10, 2013 7:44 AM
  • User350138131 posted

    By anas said I can only add the following.

    5. The namespace of the  partial class PSDiscount should be the same as the namespace in .edmx    

    Tuesday, September 10, 2013 9:21 AM
  • User4068249 posted

    Can anyone confirm whether the 'Display' data annotations are automatically used in the List, Edit and Insert templates (or possibly in the Field templates) ?

    Dynamic Data would be SO much more usable if this feature was functional. I was hoping for something more automatic than the previous suggestion,

    and I've seen examples that show the 'Display' data annotations supposedly being used although I haven't been able to get any of those examples to run.

    I'd really rather not name my SQL database columns with spaces in their names.

     

     

    Tuesday, September 10, 2013 9:34 AM
  • User1564875471 posted

    Yes I can confirm that the display attribute is supposed to work with the defualt dynamic data pages. You have something wrong. 

    Please tell us more about the version of VS, the .NET framework version you are targeting in your project(right click in the project, properties) and what is the EF version. We also assume that you are using EDMX model generated from the database.

    Also, try to add the "Dispaly" attribute directly to the entity without the partial class and see what happen. If it's still not working, try to check you might have missing reference or something wrong in the web.config file , so i suggest to create a sample project usign the dyanmic data template and see how it goes.

    Don't forget that the dynamic data project is a web application (not website), so anytime you apply a change to the .cs files, you have to re-build the project to see the changes.

    Tuesday, September 10, 2013 4:41 PM
  • User4068249 posted

    Thank you for your reply. I am using VS2010, Framework 4. I'm not sure how to determine the Entity Framework version if it can be different than the targeted Framework.

    I did try applying the attribute directly to the EF entity, and it worked! So I guess there's something about how my separate partial class is defined that is the problem.

    There  is only 1 project in the solution. There are only 2 namespaces in the solution and I've tried using both in my partial class, to no avail.

    Does my partial class need to be stored in a certain folder structure? Does the file in which it is stored have to be named a certain way?

     

    Wednesday, September 11, 2013 2:40 PM
  • User4068249 posted

    I created a new project and was very careful about naming my solution and data model namespaces consistently.

    It worked like a charm.

    Thanks for the help!

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 12, 2013 9:56 AM