如何使MVC客户端验证流利的API配置工作? [英] How to make Fluent API configuration work with MVC client side validation?

查看:81
本文介绍了如何使MVC客户端验证流利的API配置工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我preFER用流利的API配置DataAnnotation工作,因为我想从数据访问单独的模型。

I prefer working with Fluent API configuration to DataAnnotation because I want to separate model from data access.

我MVC都试过了,流利的API不与客户端验证工作。不久来讲,有一个简单的方法,使流利的API与客户端验证工作按DataAnnotation办?

I have tried in MVC, Fluent API does not work with client side validation. Shortly speaking, is there a simple way to make Fluent API works with client side validation as DataAnnotation can do?

推荐答案

没有。流利的API仅仅是映射 - 正确的。数据注解都映射和验证 - 错误。数据注解的第一EF code最差的特点之一,因为采用这种方式时,他们夫妇与持久性presentation和验证逻辑。

No. Fluent API is just mapping - correct. Data annotations are both mapping and validation - wrong. Data annotations are one of the worst features of EF code first because when used this way they couple persistence with presentation and validation logic.

建议:不要使用EF实体presentation。使用特殊的视图模型与数据的注解,让你的控制器prepare视图模型从实体,反之亦然。迟早你会发现情况下您的验证是不是1:1与你的映射或者您的视图需要比实体类型提供更多或更少的数据。使用视图模型和这些情况都会由他们来处理。

Advice: don't use EF entities for presentation. Use special view models with data annotations and let your controller prepare view models from entities and vice-versa. Soon or later you will find situations where your validation is not 1:1 with your mapping or where your view needs more or less data than provided in entity type. Use view models and these situations will be handled by them.

这篇关于如何使MVC客户端验证流利的API配置工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆