【问题标题】:How to use Oracle Entity Framework with no config file如何在没有配置文件的情况下使用 Oracle Entity Framework
【发布时间】:2015-12-02 10:38:31
【问题描述】:

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

我尝试了很多不同的东西。

目前我正在设置 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.

有没有人在不使用 crud 污染 app.config 的情况下让它工作的经验?

【问题讨论】:

    标签: oracle entity-framework entity-framework-6 odp.net


    【解决方案1】:

    是的。为了完成从 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。

    【讨论】:

      【解决方案2】:

      呃。发现问题了。

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

      多么愚蠢。

      【讨论】:

        猜你喜欢
        • 2021-08-23
        • 2014-12-05
        • 2014-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-03
        • 1970-01-01
        • 2011-02-10
        相关资源
        最近更新 更多