发现了'EFConfiguration'类型之前使用的DbConfiguration实例

use*_*747 6 c# entity-framework-6

在我的MVC4应用程序中,我参考了我的EF模型组件.一切都很好.突然间,我开始收到以下错误消息.

The default DbConfiguration instance was used by the Entity Framework before the 'EFConfiguration' type was discovered. An instance of 'EFConfiguration' must be set at application start before using any Entity Framework features or must be registered in the application's config file. See http://go.microsoft.com/fwlink/?LinkId=260883 for more information.
Run Code Online (Sandbox Code Playgroud)

我正在使用EF 6.任何想法可能是什么原因?我仔细检查数据库并更新并与EF dll同步.

更新:当我尝试实例化Context对象时,我收到此错误

mEntities context = new Entities();
Run Code Online (Sandbox Code Playgroud)

谢谢

小智 0

您的问题可能是因为您的 DAL 中有一个类似此示例的类

public class EfConfiguration : DbConfiguration
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您必须在 Global.asax 中使用 EF Context 之前对其进行初始化

参考值

  • 该错误有一个微软页面的链接,花一些时间仔细阅读你会找到答案。祝你好运!https://msdn.microsoft.com/en-us/data/jj680699 (3认同)
  • 发生这种情况的原因是您有多个使用 EntityFramework 的项目,因此实体框架具有一些全局实例化的内部类。您需要提前(在创建 DBContext 之前)告知 EntityFramework 您的 DbConfigurations。就我而言,我有 3 个使用 EF 的项目(2 个 MsSql,1 个 Oracle)。oracle 需要不同的 DbConfiguration。您需要在 OracleDbContext 上使用此 DbConfiguration,并在应用程序启动中设置以下行来告诉 EF:DbConfiguration.SetConfiguration(new OracleDbConfiguration()); (2认同)