none
how to validate the data in asp.net mvc when we use entity framework. RRS feed

  • Question

  • how to validate the data in asp.net mvc when we use entity framework.so anyone help me to explain the process.
    Friday, December 30, 2016 11:17 AM

All replies

  • how to validate the data in asp.net mvc when we use entity framework.so anyone help me to explain the process.

    https://www.asp.net/mvc/overview/getting-started/database-first-development/enhancing-data-validation

    On the other hand, the EF entity for DB first, Code first or Model first shouldn't be used at the UI. You should have custom objects like a DTO that is in the MVC View Model, and you map data between the EF entity and the View Model object and VM object back to the EF entity.

    http://www.c-sharpcorner.com/UploadFile/tirthacs/using-automapper-in-mvc/

    https://en.wikipedia.org/wiki/Data_transfer_object

    The validation scaffolding is on the properties of the DTO, and the DTO is in the MVC View Model. 

    Friday, December 30, 2016 7:45 PM
  • Hi sheshadri k,

    One of the core design tenets of ASP.NET MVC is DRY ("Don't Repeat Yourself"). ASP.NET MVC encourages you to specify functionality or behavior only once, and then have it be reflected everywhere in an application. This reduces the amount of code you need to write and makes the code you do write less error prone and easier to maintain.

    Asp.net MVC valid date via DataAnnotations, you could also change the database datatype via the following migration command.

    add-migration DataAnnotations
    update-database

    In the client side, validation detects the error and displays error message via jQuery.

    For more information, please refer to:

    https://www.asp.net/mvc/overview/getting-started/introduction/adding-validation

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 2, 2017 5:22 AM
    Moderator