0

Entity Framework generates a code file for my database .edmx, it creates for each entity .. If I customize some properties (change column names, makes required properties ... etc) as example I have the following class that generated by EF and I added some codes for it :

public partial class person
{
    public int Id { get; set; }
    public string FullName { get; set; }

    [Required(ErrorMessage = "Mother name is required!")]
    [Column("Mother Name")]
    public string MotherName { get; set; }   
}

Each time I made changes on database and generate .edmx file again, all changes that I made are gone (as commented at the top of the file.)

I can add some new properties by adding a partial class as this solution says but I can't add a partial class that contain the same property.

My question is: How can I separate my custom code from .edmx to another file?

Abdulsalam Elsharif
  • 4,773
  • 7
  • 32
  • 66
  • `change column names, makes makes required properties` these things are specified by the `edmx` file. That's where you should specify names and validations. The code generator will add the appropriate attributes to the generated properties – Panagiotis Kanavos Sep 07 '21 at 16:18
  • As the example above, If I want to add [Required] to the mother name properties, each time I regenerate the classes the [Required] will removed, So how can I do that with partial and add [Required] for mother name in a place that will not lose? – Abdulsalam Elsharif Sep 07 '21 at 16:20
  • 1
    Why don't you do that in the EDMX model? – Panagiotis Kanavos Sep 07 '21 at 16:21
  • Can you clear please? How can I do that in EDMX model? – Abdulsalam Elsharif Sep 07 '21 at 16:23
  • 1
    If you don't know that, you probably shouldn't be using EDMX. EDMX was used in *older* versions of EF and Visual Studio to map the database model to a "conceptual model" and then map that model to classes. It's an XML file but editing it was done through the [visual EF Designer](https://learn.microsoft.com/en-us/ef/ef6/modeling/designer/data-types/complex-types). Almost nothing was gained by that intermediate model though, so developers switched to `Code-First` several years ago, either generating classes directly from the database or generating tables from classes – Panagiotis Kanavos Sep 07 '21 at 16:30
  • Searching the internet for "required attribute edmx model" turns up several useful hits. I've never tried this, but... https://stackoverflow.com/questions/14059455/adding-validation-attributes-with-an-entity-framework-data-model – Flydog57 Sep 07 '21 at 16:32
  • At some point the EF Designer was removed from Visual Studio altogether. There may be extensions in the Visual Studio marketplace that can still use them, like the [EF 6 Power Tools](https://marketplace.visualstudio.com/items?itemName=ErikEJ.EntityFramework6PowerToolsCommunityEdition) – Panagiotis Kanavos Sep 07 '21 at 16:36
  • [Metadata classes](https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/database-first-development/enhancing-data-validation#add-metadata-classes) are indeed a way to "add" attributes to generated classes. They were created to allow specifying validation attributes for EF and MVC classes around 2010. Again, they fell out of widespread use as people moved to code-first. – Panagiotis Kanavos Sep 07 '21 at 16:39

0 Answers0