代码改变世界

Hundreds of errors when I change Code Generation Strategy to Default in EntityDataModel.edmx - VS2012

2013-04-24 01:15  雪中风筝  阅读(229)  评论(0编辑  收藏  举报

再看Programming Entity Framework 2th 在做书中的例子的时候,遇到了一个问题,查看自动生成的代码的时候,需要把代码生成策略从none 修改为defalut,但是修改完成以后重新编译程序的时候出现了很多错了,大概看了下,发现designer.cs中生成了代码,另外在项目目录下同时也生成了很多数据库表对应的Class,查了下原因及解决方案:

When you change the Code Generation Strategy to Default from None you are using the ObjectContext type. This type creates the entity model without the help of any templates. The Code Generation Strategy of None uses the T4 templates and is the reason for all the errors because now you are creating two classes for each table in the database. To resolve the issue delete the two templates files in Solution Explorer, the two files in the model that end with the extension .tt then recompile the project and all should be well.

参考文章:

http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/b2191ab5-b918-49c6-8583-2306b38bbf3e

但是在EF4.1版本中,EF team已经不推荐使用ObjectContext类型了,替换的是DBContext,而书中还是使用的ObjectContext。

具体内容可参考:

http://blogs.msdn.com/b/efdesign/archive/2010/06/21/productivity-improvements-for-the-entity-framework.aspx