MVC3和EF数据第一:什么是最好的做法? [英] MVC3 and EF Data first: what are the best practices?

本文介绍了MVC3和EF数据第一:什么是最好的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎最有MVC3和EF4.1的重点是围绕code第一 - 我似乎无法找到符合以下条件的任何实例或教程:

It seems that most of the focus with MVC3 and EF4.1 is around "code first" - I can't seem to find any examples or tutorials that meet the following criteria:


  • 使用现有SQLServer数据库

  • 有网络和放大器单独的项目;数据访问(我们有多个Web应用程序共享同一数据访问类)

  • 为验证的建议

确实存在这样一个例子或教程?是否有任何记录的最佳实践为如何做到这一点,或理由没有结构化的这样一个解决方案?

Does such an example or tutorial exist? Are there any documented "best practices" for how to accomplish this, or rationale for NOT having a solution structured this way?

推荐答案

这是很常见的情况,如果你想使用EDMX文件映射或者它取决于如果你想有(在code定义的映射像code首先)。

It is quite common scenario and it depends if you want to use EDMX file for mapping or if you want to have mapping defined in code (like code first).

这两种情况都可以做到第一的数据库

Both scenarios can be done as database first


  • 您将在Visual Studio中的EF工具,加强对现有数据库中创建EDMX,你会使用的DbContext T4模板生成器来获得POCO类和的DbContext派生类

  • 您会下载 EF电动工具CTP ,你会使用它的逆向工程功能生成code映射,POCO类和上下文您

  • You will create EDMX from existing database with build in EF tools in Visual Studio and you will use DbContext T4 generator template to get POCO classes and DbContext derived class
  • You will download EF Power Tools CTP and you will use its reverse engineering feature to generate code mapping, POCO classes and context for you

这两种方法都不将增加数据的注释。在实体上标注的数据不应该被用于客户端验证(这是不好的做法),除非你正在做的非常简单的应用程序。通常你的观点有一些更高级的预期和验证针对可以是不同的然后实体。例如插入视图,更新视图可以需要不同的验证,这是不可能的与实体单组数据注解的执行它。正因为如此,你应该进行验证移动数据注解专门视图模型和转换实体模型查看,反之亦然(可以使用 AutoMapper ,以简化这一点)。

Neither of these approaches will add Data annotations. Data annotations on entities should not be used for client validation (that is bad practice) unless you are doing very simple applications. Usually your views have some more advanced expectations and validation in view can be different then on entity. For example insert view and update view can need different validations and it is not possible to perform it with single set of data annotation on the entity. Because of that you should move data annotations for validation to specialized view models and transform your entities to view models and vice versa (you can use AutoMapper to simplify this).

反正它是<一个href=\"http://stackoverflow.com/questions/5128303/how-to-add-validation-to-my-pocotemplate-classes/5128373#5128373\">possible将数据添加注解生成的类通过好友班的,但提到它是不是一个好的做法。

Anyway it is possible to add data annotations to generated classes via buddy classes but as mentioned it is not a good practice.

这篇关于MVC3和EF数据第一:什么是最好的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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