面对将oracle与实体frameowrk连接的问题

时间:2015-05-14 13:41:56

标签: c# oracle entity-framework oracle11g entity-framework-6

我正在使用Oracle 11g和Entity Framework 6版本。

我遇到以下错误:

  

"从中获取提供商信息时发生错误   数据库。这可能是由实体框架使用不正确引起的   连接字符串。检查内部异常以获取详细信息并确保   连接字符串是正确的。"

我的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>

我的Db背景:

class 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; }

}

请告诉我我现在的错误。

1 个答案:

答案 0 :(得分: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 Context中添加以下代码:

protected override void OnModelCreating(DbModelBuilder modelBuilder)

        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.HasDefaultSchema("yourschemaName");

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

        }