使用创建的DbContext模型类从EF Code First生成存储库 [英] Generate repositories from EF Code First with created DbContext model classes

查看:146
本文介绍了使用创建的DbContext模型类从EF Code First生成存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开始一个新项目,并想使用EF Code First模式,但我也想使用存储库模式。我一直在寻找一种从dbcontext模型生成存储库的方法,但是到目前为止,我看到的所有答案都是从.edmx或DB生成存储库。我不想创建.edmx。

I'm starting a new project and want to use EF Code First pattern but I also want to use the repository pattern. I have been looking for a way to generate the repositories from the dbcontext models but all answers I've seen so far are for generating repositories from .edmx or DB. I don't want to create an .edmx.

我要做的步骤是:

1.编写模型

2.使用t4模板生成存储库< br>
3.从模型编写应用程序代码并回购
4.运行Add-Migration添加迁移代码

5.运行update-database根据迁移代码进行更新

The steps I want to do is this:
1. Write models
2. Generate repositories using t4 templates
3. Write app code from models and repos 4. Run Add-Migration to add migration code
5. Run update-database to update based off migration code

我可以用来告诉它基于模型类生成t4模板吗?

What could I use to tell it to generate t4 templates based on the model classes?

推荐答案

我已经找到了怎么做。使用T4Scaffolding,我将能够根据需要生成存储库。它还允许我覆盖默认的t4模板并创建自定义支架。

I've found how to do it now. Using T4Scaffolding, I am going to be able to generate the repositories as I need. It also allows me to override the default t4 templates and create custom scaffolds too.

这篇关于使用创建的DbContext模型类从EF Code First生成存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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