0

I'm Developing ASP.NET MVC Web Application project, and I'm using Entity Framework Database First Approach, So I would like to make validations on generated model classes, I know if i make validations on them directly, then my model validations will be overwritten every time my domain models are regenerated.

So I made a research, and found two approaches to use for this scenario:

1- Using buddy classes (How to add validation to my POCO(template) classes).

2- Using ViewModels and Auto-mapping them to my Entities (Designing an MVC repository using ViewModels

I see some sort of redundant code in these two methods, so, my question is:

Which one of the two approaches is best to flow?

Community
  • 1
  • 1
Abdisamad Khalif
  • 765
  • 2
  • 7
  • 19

1 Answers1

0

1) This is the correct solution for adding validation metadata for the Entity Framework objects. The validation will be triggered automatically by EF before calling SaveChanges()

2) This is an aproach for creating Data Transfer Objects from your EF objects. You normally do this when you want to return the objects to the client (like in JSON format) - and you don't want to expose all the EF specific properties (like navigation properties, primary keys etc)

Catalin
  • 11,503
  • 19
  • 74
  • 147