locked
How to Preserve Data Annotations When Doing Update from Database RRS feed

  • Question

  • User1853794821 posted

    I have a database-first-app.

    I have made extensive use of data annotations on the entities in the partial classes found under the "Model1.tt" folder.

     

    When I do "update from database" to bring in latest changes to the database structure (or to add a new table to the entity model), existing Data Annotations get removed because the <entityname>.cs file gets overwritten (even though there were no changes to the <entityname>.

    Am I missing something?

    In the early stages of development (and in an ongoing production environment), database changes are likely to happen.  It seems there shojuld be something to allow one to avoid redoing all of the tedious work involved in getting all the data annotations "just right"

    Tuesday, December 6, 2011 5:50 PM

Answers

  • User197322208 posted

    add to the Model1.tt , not to the .cs itself.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 6, 2011 5:54 PM
  • User197322208 posted

    Try editing the .tt file

    Puts some comment there

    #>

    //this is a comment

    <#

    -and see appearing on the .cs files

    Later Edit: see http://www.hanselman.com/blog/T4TextTemplateTransformationToolkitCodeGenerationBestKeptVisualStudioSecret.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 6, 2011 6:03 PM
  • User-552280406 posted

    I do this by creating "buddy classes", which are basically NEW partial classes you create with a metadata type of another class.. and on that class you will put all of your data annotations, so when you update your model and the db changes, this class will NOT be affected and you will keep your data annotations..

    This is explained in the following link in the first answer in a very simple way

    http://stackoverflow.com/questions/4915957/using-system-componentmodel-dataannotations-with-entity-framework-4-0/

    Remember to mark as answer if it helps. :D

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 7, 2011 3:03 AM
  • User1853794821 posted

    Thanks.  Yes, it is a file.  My bad.

    However, editing that thing is way, way beyond my pay grade.  It may seem a solution to some, but not to this noob.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 7, 2011 7:13 AM

All replies

  • User197322208 posted

    add to the Model1.tt , not to the .cs itself.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 6, 2011 5:54 PM
  • User1853794821 posted

    I am not sure what you mean by your reply.

    What is  "add to the Model1.tt"

    I am a beginner in this MVC3 and C# in general.  Please elaborate on your reply.

    Sorry to be so dumb.

    Tuesday, December 6, 2011 6:01 PM
  • User197322208 posted

    Try editing the .tt file

    Puts some comment there

    #>

    //this is a comment

    <#

    -and see appearing on the .cs files

    Later Edit: see http://www.hanselman.com/blog/T4TextTemplateTransformationToolkitCodeGenerationBestKeptVisualStudioSecret.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 6, 2011 6:03 PM
  • User1853794821 posted

    I am sorry.  I cannot find "the tt file" in my project.  Even with hidden files viewing on; no file exists in my project with the "tt" extension other than the folder named "Model1.tt"

    Either I am really dumb or maybe I have not explained my problem clearly.

    I don't know anything about tt files and would dare not try and engineer a T4 solution to my problem.

     

    My problem, again, is that whenever I right click in the entity model and click on "update model from database" , after going through the wizard, and I click "Finish" button in the wizard, the first time I do a Save on the model or do a build on the project, I get the popup box that running the text template could do evil.  I check OK and I get the box agaion.  I check OK.    At this point, all of my data annotations are gone.  This is not what should IMHO be happening.

    The wizard does not give me control over what tables should be refreshed.  The only thing I have control over is whether or not I want to add some new table(s) to my model.  

    I tried to muddle through the hanselmann blog entry but I could not see what, if anything, it had to do with my problem (even were I to understand what was in his blog).

    So is there no simple solution to the problem?  It would appear to me that model refreshes will always be expected to be needed over the life of an MVC3 APP and that such drastic loss of the data annotations on the model upon model refresh greatlyly reduces the value of data annotations and would lead to a much higher probability of the application's breaking on every model refresh because the redo of one or more trashed annotations was overlooked.

    Tuesday, December 6, 2011 7:09 PM
  • User197322208 posted

    other than the folder named "Model1.tt"

    Are you sure it's a folder and not a file ?  Look with windows explorer ... or double click the .tt folder. I think it's a file , not a folder

    My problem, again, is that whenever I right click in the entity model and click on "update model from database" , after going through the wizard, and I click "Finish" button in the wizard, the first time I do a Save on the model or do a build on the project, I get the popup box that running the text template could do evil.  I check OK and I get the box agaion.  I check OK.    At this point, all of my data annotations are gone.  This is not what should IMHO be happening.

    It's a .tt file!

    It would appear to me that model refreshes will always be expected to be needed over the life of an MVC3 APP and that such drastic loss of the data annotations on the model upon model refresh greatlyly reduces the value of data annotations

    Again - the .cs files are generated by the .tt file! And re-generated each time you modify the model.edmx!

    Tuesday, December 6, 2011 7:16 PM
  • User-552280406 posted

    I do this by creating "buddy classes", which are basically NEW partial classes you create with a metadata type of another class.. and on that class you will put all of your data annotations, so when you update your model and the db changes, this class will NOT be affected and you will keep your data annotations..

    This is explained in the following link in the first answer in a very simple way

    http://stackoverflow.com/questions/4915957/using-system-componentmodel-dataannotations-with-entity-framework-4-0/

    Remember to mark as answer if it helps. :D

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 7, 2011 3:03 AM
  • User1853794821 posted

    Thanks.  Yes, it is a file.  My bad.

    However, editing that thing is way, way beyond my pay grade.  It may seem a solution to some, but not to this noob.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 7, 2011 7:13 AM
  • User1853794821 posted

    Thanks.  This looks like it mioght be the answer. 

    I shall need to spend some time getting my head around this (simple to you, but still rocket science to me).  It looks like a more feasible approach than the brain surgery approach of operating on the tt file.

    I am hoping this will work for ALL of the data annotations. 

    Couple questions.

    Where (what folder) should I put the buddy classes?  I imagine I would want them sonewhere in a folder that: (1) doesn't get wiped out by model refreshes and (2) that lets them get compiled into the solution.

    Should I just put them all in a single cs file?

    Wednesday, December 7, 2011 7:18 AM
  • User-552280406 posted

    Sorry for the late reply.. I have them under the Models folder in a subfolder named however you want. (Validation, BuddyClasses, w/e). It wont be affected by the data model being refreshed.

    I use one CS file per model

    Sunday, December 11, 2011 3:30 PM