本文介绍了MVC3 和实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单:将 .edmx 文件放在 MVC3 项目的 Web 应用程序的模型文件夹中是否是一个好习惯?

My question is pretty simple: is it a good practice to place the .edmx file in the model folder in a Web application of an MVC3 project?

推荐答案

我的回答很简单,不要把表示层(整个 MVC 应用程序)与数据访问逻辑和数据建模搞混.

my answer is pretty simple, do not mess up presentation layer (Whole MVC application) with data access logic and data modeling.

在您的 Visual Studio 解决方案中至少有 4 个项目,从下到上:

Have at minimum 4 projects in your Visual Studio Solution, from bottom to the top:

1 - ProjectName.Interfaces(类库,实体的接口);

1 - ProjectName.Interfaces (Class library, entities's interfaces);

2 - ProjectName.DAL(类库,唯一一个知道使用了 EF 的库,POCO 实体使用另一个文件实现项目 1 的接口,在该文件中您使用部分类重新声明相同的对象......);

2 - ProjectName.DAL (Class library, the only one allowed to even know the EF is used, the POCO entities implement the interfaces of project 1 using another file where you redeclare same objects using partial classes...);

3 - ProjectName.BL(类库,业务逻辑,引用上面1和2的两个项目);

3 - ProjectName.BL (Class library, Business logic, references the two projects above 1 and 2);

4 - ProjectName.Web(ASP.NET MVC 应用程序,表示层,引用两个项目 1 和 3 但不是 2);

4 - ProjectName.Web (ASP.NET MVC application, Presentation Layer, references two projects 1 and 3 but NOT 2);

这当然是为了简化事情,根据我的经验,这是一个可靠的设计,对于非常小的项目来说有点矫枉过正,但从长远来看是值得的.

this to simplify things of course, based on my experience this is a solid design, a bit overkilling for very small projects but pays off in the long term.

在我看来,MVC的M,Model,不是数据模型,不是EF,不是做ORM绑定到具体的数据库引擎.

in my view, the M of MVC, Model, is NOT the data model, is not the EF, is not there to do ORM bound to the specific database engine.

这个答案当然是主观的,是基于我的个人经验;-)

this answer is subjective of course and is based on my personal experience ;-)

这篇关于MVC3 和实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 22:00