MVC4中使用EntityFramework
首先,MVC4不支持EntityFramwok 6版本,如果安装了EntityFramwok 6,则vs不会自动引用dll和修改web.config配置。
先删除旧的版本,执行指令:
Uninstall-Package EntityFramework.zh-Hans
Uninstall-Package EntityFramework [-force]
如:
然后,
通过NuGet安装EntityFramwok,可以指定要使用的版本,如(MVCEF1为项目名称):
Install-Package EntityFramework -ProjectName MVCEF1 -Version 5.0
执行成功后,web.config配置如下:
<configSections>
<section
name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,
EntityFramework, Version=4.4.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
......
<entityFramework>
<contexts>
<context type="MVCEF1.DAL.AccountContext,MVCEF1">
<databaseInitializer type="MVCEF1.DAL.AccountInitializer,MVCEF1"></databaseInitializer>
</context>
</contexts>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
参考:
http://www.mamicode.com/info-detail-128217.html