0

I have downloaded the MVCScaffolding nuget package within VS2010. I am trying to retrieve a [DisplayName data annotation from my model in order to use it within the index.cs.t4 template.

This page OneToMany Relationships has shown me how to modify the index template in order to provide a link which will take me to the controller index for my child objects. Which in this case from emails to emailrecipients. The problem is i have called my controllers emailcontroller and emailrecipientscontroller rather than their rather less descriptive tables names which i would prefer to hide anyway. i have equally decorated the metadataobject which the t4 template uses(tbl_My_unwieldytablename_emailMetadata) with this displayname (emailrecipients) dataannotation and i was hoping i could modify the template in order to replace the name for the relation to use the displayname which is my controller name i.e.

[MetadataType(typeof(tbl_My_unwieldytablename_emailMetadata))]
public partial class tbl_My_unwieldytablename_email
{
    internal sealed class tbl_My_unwieldytablename_emailMetadata
    {
        [ScaffoldColumn(false)]
        [Required(ErrorMessage="id is required")]
    public Int32 id { get; set; }

        [DataType(DataType.Date)]
    public DateTime send_date { get; set; }

        [StringLength(255)]
    public String title { get; set; }

        [DataType(DataType.MultilineText)]
    public String message { get; set; }

        [StringLength(50)]
    public String author { get; set; }

        [StringLength(80)]
        [DataType(DataType.EmailAddress)]
    public String author_email { get; set; }

        [DataType(DataType.MultilineText)]
    public String attachment { get; set; }

        [DataType(DataType.Date)]
    public DateTime created_date { get; set; }

    public Int32 batches { get; set; }

        [DataType(DataType.Date)]
    public DateTime complete_date { get; set; }

    [DisplayName("emailrecipients")]
    public EntityCollection<tbl_My_unwieldytablename_email_recipients> tbl_My_unwieldytablename_email_recipients { get; set; }

    }

Cheers Tim

Tim
  • 7,401
  • 13
  • 61
  • 102

1 Answers1

-1
[Table("tbl_My_unwieldytablename_email")]
Public Class Email

[Table("tbl_My_unwieldytablename_email_recipients")]
Public Class EmailRecipients

Should map your classes to the database tables

Mike
  • 1,199
  • 1
  • 15
  • 24
  • im confused as to where you think i should do this or what it will help with? Are you referring to the controller classes? – Tim Jun 04 '12 at 08:27
  • It should map the unwieldy table names to your class names. Since you already have the tables in the database, if you add the data attribute table("tablename") above the classes, then mvc will map them so that your classes can be named with something less unwieldy. For example '[Table("tbl_My_unwieldytablename_email")] public partial class Email' This will also automatically let mvc know to use emailcontroller. – Mike Jun 04 '12 at 17:59
  • Hi Mike , not sure this will suit my situation, my metadata classes have the actual table names but the relationships i have decorated with the friendly names. its the mapping in the scaffolded views (rather than the scaffolded metadata classes) which i wantedto modify. If i could find a way to decorate detect and decorate the foreign key fields as in the one to many page i put a link to above then this would sort things. but at the moment im a bit stumped with the t4 template syntax. – Tim Jun 05 '12 at 18:56
  • 1
    I think I'm going to need more details. Currently, you have no defined foreign key data attribute properties in this class. You would need to define them. The displayname attribute in the index.cs.t4 template gets tossed into the property.Name string. You may want to look at [link](http://www.hanselman.com/blog/T4TextTemplateTransformationToolkitCodeGenerationBestKeptVisualStudioSecret.aspx) – Mike Jun 09 '12 at 03:44
  • This answer appears to have nothing to do with the question (retrieving the `DisplayName` value in a T4 template file). – Extragorey Feb 18 '19 at 22:55