0

I have created a database first, then created an Entity Data Model (.edmx) file. Now I've added some validation to a few of my Model files (ending with *.cs).

Now I did no changes in the "database", just added a View for multiple tables that join together a few tables that are relational to eachother. That is, in "Server Explorer" -> "Data Connections" -> "View" -> "Add New View" to create a Joined View so that multiple tables can be join into one view.

Also, this has happened just by dragging the Diagram to "look good" so I can easily see how they relate and then right after I noticed all my validation were, yet again - GONE. I'm guessing this is not the only thing that will "disappear" whenever I do some changes that in reality are no changes to the actual database.

[Required, MaxLength(50, ErrorMessage = "Title can only be 50 characters long")]
public string Title { get; set; }

This line is removed when I just realign my Diagram, or whenever I do something that does not alter this specific Model in the database whatsoever. I did not add a new Column, I did not change a not on the database, I did not drop anything. I dragged the Diagram and I added something not relative this Model.

How do I stop this from happening? Or is validation the last thing you do? Shouldn't matter, because then after a project has been finished you want to add another table (not altering old ones) you have to recreate validation again and again? Sounds like a pain in the ass. Considering you could have 200 models and plenty of validation on each of them, not to speak of all the "Regular Expressions" that takes some time to write.

To verify, I just tried it again. Added that line over there to one of my Model.cs and dragged my "View" on the Diagram one milimeter and it recreates everything.

Deukalion
  • 2,516
  • 9
  • 32
  • 50
  • 1
    You are not supposed to edit the generated code at all. If you want to add validation attributes you can create meta data classes that will be untouched when the code is regenerated http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.metadatatypeattribute%28v=vs.110%29.aspx – Ben Robinson Nov 26 '14 at 16:31
  • Creating a seperate Meta class and then adding [MetadataType(typeof(MyClass)] removed that instead of all the data. So now I have to input all those eachtime it's regenerated? Seems like a lousy solution that still requires me to redo work that I've already done, when MVC is suppose to be the opposite. – Deukalion Nov 28 '14 at 10:02
  • No the point is with meta classes, you don't have to re-do it each time it is regenerated. The meta classes are not affected when the entity classes are regenerated so after the initial effort of moving your attributes from the generated code, where they never should have been in the first place, you only have to touch the meta classes if your model actually changes (i.e. fields are added removed or modified), or you want to alter the validation rules. – Ben Robinson Nov 28 '14 at 10:04
  • Yes, that I understand. Still, adding MetatypeData(typeof(Class)) takes sometime in larger projects. It would instead have been nicer with a cleaner solution where I can just tell the compiler that it shall reflect the attributes for one class for another class when compiling, or at runtime. Something separate from both of the Model and the Meta class. But I were hoping. At least now I don't have to redo all my regular expressions. – Deukalion Nov 28 '14 at 10:21

0 Answers0