Where to run a duplicate check for an entity

前端 未结 3 1968
灰色年华
灰色年华 2020-11-27 21:13

I\'m looking for advice on the \"best\" place to put validation logic, such as a duplicate check for an entity, when using Entity Framework Code-First, in an MVC application

3条回答
  •  清酒与你
    2020-11-27 22:03

    The most reliable place to perform this logic would be the database itself by declaring an unique field constraint on the name column. When someone attempts to insert or update an existing entity and try to set its name to an existing name a constraint violation exception will be thrown that you could catch and interpret in your data access layer.

提交回复
热议问题