ASP.NET MVC4 & Entity Framework 6.0 IIS 部署出错解决方案

本文介绍了一种解决MVC4应用中Entity Framework在IIS部署后出现加载错误的方法。通过在Model1.Context.cs文件中添加一个从未调用的方法,成功解决了部署后的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

博客地址 http://blog.youkuaiyun.com/foxdave

近期了解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的上下文代码CS文件(Model1.Context.cs)中添加这个方法

[csharp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. public void FixEfProviderServicesProblem()  
  2. {  
  3. //The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer'  
  4. //for the 'System.Data.SqlClient' ADO.NET provider could not be loaded.   
  5. //Make sure the provider assembly is available to the running application.   
  6. //See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.  
  7.   
  8. var instance = System.Data.Entity.SqlServer.SqlProviderServices.Instance;  
  9. }  

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

此解决方案还需要后续进行观测,毕竟修改的是根据数据库生成的文件。

还有一种方案我没有尝试,可以强制部署EntityFramework.SqlServer.dll这个文件到输出目录,有兴趣的可以尝试。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值