简体   繁体   English

ASP.NET MVC 4实体框架数据库第一个存储库

[英]ASP.NET MVC 4 Entity Framework Database First Repository

I've been reading Pro ASP.NET 4 and the demo application they use the Entity Framework code first. 我一直在阅读Pro ASP.NET 4及其演示应用程序,它们首先使用Entity Framework代码。 In my project I've already got the database. 在我的项目中,我已经有了数据库。 In the book they only deal with one table, then create an ITableNameRepository for it, then pass that into the controller's constructor. 在这本书中,他们只处理一个表,然后为其创建一个ITableNameRepository ,然后将其传递给控制器​​的构造函数。 Is it even right in the first place to make a repository with a model of your entire database? 首先,使用整个数据库的模型来构建存储库是否正确? Should I just pass an instance to each controller's constructor of IMyDataModel ? 我是否应该将实例传递给IMyDataModel每个控制器的构造IMyDataModel

There's a clue in the name of the interface. 接口名称中有一个线索。 They called it ITableNameRepository rather than IDatabaseNameRepository 他们把它叫做ITableNameRepository而非IDatabaseNameRepository

In my application I pass in an IUnitOfWork and that has the various repositories as properties, so the controller's all have a similar constructor signature that doesn't need to change when a controller needs to access a new repository. 在我的应用程序中,我传入了一个IUnitOfWork ,它具有各种存储库作为属性,因此控制器的所有存储库都具有类似的构造函数签名,当控制器需要访问新的存储库时,无需进行更改。

The DbContext class is itself a combination of the Unit Of Work and Repository patterns with DbSet s acting as repositories and SaveChanges() implementing the unit of work. DbContext类本身是工作单元和存储库模式的组合,其中DbSet充当存储库,而SaveChanges()实现工作单元。

The DbContext or IUnitOfWork does mean the controller can access the entire database - but via several repositories not one big one. DbContextIUnitOfWork确实意味着控制器可以访问整个数据库-但通过多个存储库而不是一个大的存储库。 I use a repository for each table but I have seen many people advocate "aggregate roots" 我为每个表使用一个存储库,但我看到许多人主张“聚合根”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET MVC 5应用程序-没有实体框架的存储库模式 - ASP.NET MVC 5 application - Repository pattern without Entity Framework 如何使用“代码优先实体框架”在ASP.NET MVC 4中创建数据库并将其连接到数据库? - How to create a database and connect to it in ASP.NET MVC 4 by using Code First Entity Framework? 如何首先在ASP.NET MVC中将Identity与Entity Framework数据库一起使用 - How to use Identity with Entity Framework database first in ASP.NET MVC ASP.NET MVC - 如何首先使用实体框架模型/数据库部署到 Azure? - ASP.NET MVC - How to Deploy to Azure using Entity Framework Model/Database First? 代码优先实体框架Asp.net MVC - Code First Entity Framework Asp.net MVC 具有实体框架代码优先导航属性的ASP.Net MVC - ASP.Net MVC with Entity Framework Code First Navigation Property PopUp 不在 ASP.NET MVC 5 和实体框架中的数据库中发送数据 - PopUp not sending data in database in ASP.NET MVC 5 and Entity Framework ASP.net MVC与Entity Framework重新排序数据库记录 - ASP.net MVC with Entity Framework reordering database records 使用 asp.net mvc 4 + Entity Framework 将图像保存到数据库 - Saving images to database with asp.net mvc 4 + Entity Framework 配方-Entity Framework ASP.NET MVC中的成分数据库 - Recipe - ingredients database in Entity Framework ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM