1

我正在尝试使用 Entity Framework 5.0 实现数据库优先方法,但不知何故我没有做对。我有以下疑问需要澄清。

1.添加 Ado.Net 实体数据模型后,会在“Model.Context.tt”下的文件夹中自动创建一个 DBContext 类“Model.Context.cs”。我需要再次添加 DbContext 生成器吗?(我发现其他人推荐了这个,但我不知道为什么!)

2.如何从 edmx 文件中搭建一个控制器?假设我有一个实体,比如 A(我想将其搭建到控制器),与实体 B 具有一对多关系,我将在哪里定义这种关系?在从 edmx 文件自动生成的模型类中,还是我创建类 A 和 B 并再次定义然后脚手架模型 A?

任何帮助将不胜感激。谢谢

4

1 回答 1

1

Abhatt:

  1. What t4 templates do is generating classes for you and you need to keep them, unless you decide to use another t4 template. For instance, you may want to design you database but after that decide to use code first to take advantage of code first approach, in that case after designing the database you will add another t4 template named "EF 5.x DbContext Fluent Generator for C#" and that template creates the poco class and all mappings for you.

  2. Whenever you are adding a controller mvc uses scaffolding to create controller's methods and views. However, if you want to have more control on how to generate them, you may install MVCScaffolding from package manager console. Having MVCScaffolding installed, you will be able to customize t4 templates. For more info check out MVC Scaffolding project on CodePlex: http://mvcscaffolding.codeplex.com/ also there is another good one: http://www.codeproject.com/Articles/468777/Code-First-with-Entity-Framework-5-using-MVC4-and

于 2013-11-15T11:55:41.200 回答