深入探索ASP.NET MVC应用与Entity Framework的结合
1. 用存储库替换上下文
在构建MVC模型时,可以使用EntityObjects构建一个或多个层,但这里选择使用之前在Web Forms应用程序中使用的存储库,它非常适合在MVC应用程序中使用。
1.1 操作步骤
- 移除引用 :移除对BreakAway模型的引用。
- 添加引用 :添加对以下项目的引用:
- BreakAwayEntities
- Interfaces
- POCOState
- Repositories
- 清理并重建解决方案 :确保没有对先前模型项目的引用残留。
- 移除System.Data.Entity引用 :从MvcApplication项目中移除对System.Data.Entity的引用,因为它不再与Entity Framework绑定。
1.2 代码示例
以下是一个ContactController的示例代码:
public class ContactController : Controller
{
private UnitOfWork uow = new UnitOfWork