如何在没有配置文件的情况下使用Oracle Entity Framework

问题描述:

是否有可能创建一个代码优先的实体框架模型,该模型使用ODP.Net连接到现有数据库,而无需在app.config文件中进行任何设置?如何在没有配置文件的情况下使用Oracle Entity Framework

我尝试过很多不同的事情。

目前我设置DbConfiguration:

sealed class EntityFrameworkConfiguration : DbConfiguration 
    { 
     public static readonly DbConfiguration Instance = new EntityFrameworkConfiguration(); 

     EntityFrameworkConfiguration() 
     { 
      this.SetDefaultConnectionFactory(new OracleConnectionFactory()); 
      this.SetProviderServices("Oracle.ManagedDataAccess.Client", EFOracleProviderServices.Instance); 
     } 
    } 

    DbConfiguration.SetConfiguration(EntityFrameworkConfiguration.Instance); 

我传递一个OracleConnection直接进入EF上下文。

不过,我要么在SQL Server中的格式来产生与SQL问题(周围使用表的别名双引号),或者出现以下错误:

An unhandled exception of type 'System.NotSupportedException' occurred in EntityFramework.dll 

Additional information: Unable to determine the provider name for provider factory of type 'Oracle.ManagedDataAccess.Client.OracleClientFactory'. Make sure that the ADO.NET provider is installed or registered in the application config. 

有没有人得到这个的任何经验在不污染app.config的情况下工作吗?

Uff。发现问题。

因为我使用小写注册列映射,查询不起作用。列和表名必须大写。

多么愚蠢。

是的。要完成从machine.config/app.config到基于代码配置的切换,我还必须包含对SetProviderFactory()的调用。

public sealed class EntityFrameworkConfiguration : DbConfiguration 
{ 
    public static readonly DbConfiguration Instance = new EntityFrameworkConfiguration(); 

    public EntityFrameworkConfiguration() 
    { 
     SetDefaultConnectionFactory(new OracleConnectionFactory()); 
     SetProviderServices("Oracle.ManagedDataAccess.Client", EFOracleProviderServices.Instance); 
     SetProviderFactory("Oracle.ManagedDataAccess.Client", new OracleClientFactory()); 
    } 
} 

我也是在我的应用程序启动时调用DbConfiguration.SetConfiguration(EntityFrameworkConfiguration.Instance);因为我的DbContext在所有共享此配置需要多个组件。

在附注中,我也发现这对于允许您的应用程序在ConfigurationErrorsException: The 'DbProviderFactories' section can only appear once per config file范围内工作,以防您可能无法修复用户的machine.config的情况有效。