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?

8
  • 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 Commented Sep 7, 2021 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? Commented Sep 7, 2021 at 16:20
  • 1
    Why don't you do that in the EDMX model? Commented Sep 7, 2021 at 16:21
  • Can you clear please? How can I do that in EDMX model? Commented Sep 7, 2021 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. 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 Commented Sep 7, 2021 at 16:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.