I have EDM model classes generated by entity FrameWork DB first approach. Everytime I update EDMX all the classes that have I added validation,schema, attribute names to be displayed are gone. What is the best approach to handle this situation ?
Asked
Active
Viewed 135 times
1 Answers
0
The generated classes are partial, so you can generate your own properties and methods into a new partial class (in different file) for associate generated class.
Hope my answer could help.

OO7
- 660
- 4
- 10
-
I tried but I am having The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type. Do not know how to do. I have added a new picture that you can see. – QuestforTheRightAnswer Jan 07 '20 at 20:14
-
Understood, but take a look for this one: https://stackoverflow.com/questions/850404/using-partial-classes-in-entity-framework-with-custom-properties – OO7 Jan 07 '20 at 21:04
-
I found this link and I am going to try this one :https://stackoverflow.com/questions/4915957/using-system-componentmodel-dataannotations-with-entity-framework-4-0/ – QuestforTheRightAnswer Jan 08 '20 at 02:21
-
It seems EF will overwrite any custom, it doesn't keep. But pls to correct me if EF no overwrite. – OO7 Jan 08 '20 at 08:52
-
all you got to do is 1) create a separate folder, add a class and add your validation, column names (redeclaration of properties etc.,) there 2) in the partial model setup metadatatype. If the edmx gets updated, (red color part go away) ( green color wont ) see my images. – QuestforTheRightAnswer Jan 08 '20 at 17:51
-
It's look no need to redefine all columns in class metadata. And need another partial class (same name with class model) in different file and different location. Take a look this one: https://stackoverflow.com/questions/38940443/does-it-make-sense-to-use-metadatatype-to-enforce-validations-in-case-of-code-fi – OO7 Jan 08 '20 at 19:44
-
One big problem for static classes (that produce static models) is... it couldn't be dynamic models on the fly. – OO7 Jan 08 '20 at 20:06
-
make sense. can you give me some sample code by using TypeDescriptor.AddProvider(new AssociatedMetadataTypeTypeDescriptionProvider( typeof(ExternalEntity), typeof(ExternalEntityMetadata), typeof(ExternalEntity)); – QuestforTheRightAnswer Jan 08 '20 at 20:22
-
You can see the sample and explaination here: https://blogs.msdn.microsoft.com/davidebb/2009/07/24/using-an-associated-metadata-class-outside-dynamic-data/ – OO7 Jan 08 '20 at 21:44