2015-05-14 23 views
1

我正在使用Oracle 11g和Entity Framework 6版本。面向oracle連接實體框架問題

我對着下面的錯誤:

"An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct."

我App.Config中如下:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections> 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
    <section name="entityFramework" 
     type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/> 

    <section name="oracle.manageddataaccess.client" 
     type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342"/> 
    </configSections> 
    <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/> 
    </startup> 
    <connectionStrings> 
    <clear/> 

    <add name="OracleDbContext" providerName="Oracle.ManagedDataAccess.Client" 

     connectionString=" Data Source=HRFOLATEST1;User ID=hrms2;Password=hrms2;"/> 

    </connectionStrings> 
    <entityFramework> 

    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/> 

    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/> 

     <provider invariantName="Oracle.ManagedDataAccess.Client" 
     type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=6.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342"/> 

    </providers> 

    </entityFramework> 

    <system.data> 

    <DbProviderFactories> 

     <remove invariant="Oracle.ManagedDataAccess.Client"/> 

     <add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver" 

     type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342"/> 

    </DbProviderFactories> 

    </system.data> 

    <runtime> 

    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 

     <dependentAssembly> 

     <publisherPolicy apply="no"/> 

     <assemblyIdentity name="Oracle.ManagedDataAccess" publicKeyToken="89b483f429c47342" culture="neutral"/> 

     <bindingRedirect oldVersion="4.121.0.0 - 4.65535.65535.65535" newVersion="4.121.2.0"/> 

     </dependentAssembly> 

    </assemblyBinding> 

    </runtime> 

</configuration> 

我的數據庫環境:

類DatabaseContext:的DbContext

{ 
    public DatabaseContext() : base("OracleDbContext") 
    { 

    } 
    public DbSet<User> Users { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     //Configure domain classes using modelBuilder here 

     modelBuilder.Entity<User>().ToTable("HRMS_OLAS_TREE"); 

     modelBuilder.Entity<User>().Property(user => user.ID).HasColumnName("EMP_ID").HasColumnType("VARCHAR"); 

     modelBuilder.Entity<User>().Property(user => user.NAME).HasColumnName("EMP_NAME").HasColumnType("VARCHAR"); 

     base.OnModelCreating(modelBuilder); 

    } 
} 

internal class User 
{ 
    public long ID { get; set; } 
    public string NAME { get; set; } 

} 

Ple現在讓我知道我在做什麼錯誤。

+0

可能與如何tnsnames.ora中被解決。 Cound嘗試將這些添加到連接字符串中。 http://www.connectionstrings.com/oracle/ –

+0

內部異常: {「提供程序沒有返回ProviderManifestToken字符串。」} 我能夠使用實體框架從.Net應用程序連接到Oracle。 我在嘗試通過實體框架進行連接時遇到了問題。使用相同的連接字符串 –

回答

1

我已經改變了連接字符串以下,並開始工作:

<add name="OracleDbContext" providerName="Oracle.ManagedDataAccess.Client" 
     connectionString=" Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=yourhostname)(PORT=yourportnumber))(CONNECT_DATA=(SERVICE_NAME=""servicename))); User Id=xxx;Password=xxxx;"/> 

我們可以找到tnsnames.ora文件這些細節。

以dB爲單位語境中添加以下代碼:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 

     { 
      base.OnModelCreating(modelBuilder); 

      modelBuilder.HasDefaultSchema("yourschemaName"); 

      modelBuilder.Configurations.Add(new EmployeeMapper()); 

     }