MVC3验证与实体框架模型/数据库优先 [英] MVC3 Validation with Entity Framework Model/Database First

查看:149
本文介绍了MVC3验证与实体框架模型/数据库优先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用MVC 3,实体框架为我的申请。

I want to use MVC 3 and the Entity Framework for my application.

该模型将被存储在不同的组装到MVC应用

The model will be stored in a different assembly to the MVC app.

我做的选择是要么使用EF生成我的实体或先用code。

The choice I'm making is either to use the EF to generate my entities or to use code first.

先用code,我可以装饰与会员[必填]等...但我将如何去EF是否已产生从数据库中添加实体的属性?

With code first, I can decorate members with [Required] etc... But how would I go about adding those attributes if EF has generated entities from the DB?

有EF生成我的实体将节省大量的时间,但我想MVC自动填充验证取决于如何,我饰我的成员。这是否有意义?如果是的话,我会怎么做呢?

Having EF generate my entities will save a lot of time, but I want MVC to auto populate the validation depending on how I've decorated my members. Does this make sense? If so, how would I do that?

推荐答案

在这种情况下,<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.metadatatypeattribute.aspx\">MetadataTypeAttribute用来。您可以使用部分类结合起来,以达到预期的效果。

In that case, MetadataTypeAttribute is used. You can combine it with partial classes to achieve desired results

顺便说一下,在你的地方,我会使用数据库优先和code首先设计之间作出决定时,做更多的研究。这一切是不是生成实体时节省时间,有这两种方法之间的更差。为了节省时间的目的,你可以使用 EF电动工具来生成数据库code第一实体 - 简单的

And by the way, in your place i would do more research when deciding between using Database First and Code First designs. That all is not about saving time when generating entities, there's much more difference between those two approaches. For the time saving purpose, you can use EF Power Tools to generate code first entities from database - simple.

这篇关于MVC3验证与实体框架模型/数据库优先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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