none
How to modify a few generated properties in a single class? RRS feed

  • Question

  • Hello,

    We have an ASP.NET MVC4 application that is using Entity Framework 5.  In one of the generated classes, there are 32 properties, but there are 4 properties that we want to change whenever the class is generated.

    Currently, the following code is generated, and the properties map back to a database table:

    public string field1 { get; set; }
    public string field2 { get; set; }
    public string field3 { get; set; }
    public string field4 { get; set; }

    We want to generate the following that only permit
    getting a custom value.  Please note that they do
    not use the underlying database table.
    public string field1 { get {return "a"; set {} }
    public string field2 { get {return "b"; set {} }
    public string field3 { get {return "c"; set {} }
    public string field4 { get {return "d"; set {} }

    To validate that everything will work okay, I manually
    edited the generated POCO class and ran our application.
    Everything worked well.  

    Now, I would like to make these changes automatically
    appear whenever the class is generated.  I think that
    I will need to edit some kind of template/T4 file, but
    I did not find any details when I Googled.  I only want
    to modify a single, generated class.  

    Is it possible to make this work?

    I have not edited T4 files before, so I would appreciate any extra details... :)

    Thank you for your time and help,

    Mike

    Monday, May 6, 2013 2:24 PM

Answers

  • Hi A Bit of Help,

    Yes, you can try to modify T4 template. If you have get the entity model, you may find two .tt file in .edmx. For example, Model1.Context.tt and Model1.tt. The first file is used to generate the DbContext, the second file is used to generate the POCO classes. So we can modify the Model1.tt file.

    We can start with the code snippet in Model1.tt file:

    foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection))
    {
        fileManager.StartNewFile(entity.Name + ".cs");
        BeginNamespace(code);
    #>

    We can see each new file is created by StartNewFile method. BeginNamespace method is used for generating namespace code. I have tested to add the following code before: foreach (var navigationProperty in navigationProperties):

    <# if (entity.Name == "User") { #>
        public string field1 { get { return "a"; } set { } }
        public string field2 { get { return "b"; } set { } }
        public string field3 { get { return "c"; } set { } }
        public string field4 { get { return "d"; } set { } }
    <# } #>

    After saving the file, the new generated User class looks like:

    public partial class User
    {
        public int UserID { get; set; }
        public string Name { get; set; }
    
        public string field1 { get { return "a"; } set { } }
        public string field2 { get { return "b"; } set { } }
        public string field3 { get { return "c"; } set { } }
        public string field4 { get { return "d"; } set { } }
    
        public virtual UsersDetail UsersDetail { get; set; }
    }
    

    Best regards,

     

    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 8, 2013 3:08 AM
    Moderator