MVC4程序运行报错

近期了解MVC4的时候弄了一个简单的小工程,使用Entity Framework作为Model,F5启动调试运行的时候没有问题,但是发布到IIS之后访问就报错

错误信息如下:

The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application.

很奇怪,搜索了一下,找到一个老外的帖子,戳这里

解决方案如下:

首先检查EF相关的程序集是否被引用,主要有两个:EntityFramework、EntityFramework.SqlServer (在这儿要注意两者的版本号是否一致,二者存在依赖关系,版本不一致的话,会报其他错)

在EF的上下文代码CS文件(Model1.Context.cs)中添加这个方法

public void FixEfProviderServicesProblem()  
{  
//The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer'  
//for the 'System.Data.SqlClient' ADO.NET provider could not be loaded.   
//Make sure the provider assembly is available to the running application.   
//See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.  
  
var instance = System.Data.Entity.SqlServer.SqlProviderServices.Instance;  
}  

不明所以,具体原因还是不清楚,这个方法也从来未被调用过,但是。。。好用了!

原文地址:https://www.cnblogs.com/chenwolong/p/EntityError.html