2015-09-06 73 views
2

是否有可能創建一個代碼優先的實體框架模型,該模型使用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的情況下工作嗎?

回答

1

Uff。發現問題。

因爲我使用小寫註冊列映射,查詢不起作用。列和表名必須大寫。

多麼愚蠢。

0

是的。要完成從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的情況有效。

相關問題