2015-05-14 26 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()); 

     }