在 C# 中使用 Entity Framework 读取 Oracle 数据连接

Read Oracle data connection using Entity Framework in C#

我有 Oracle 数据库 11g,我想使用 Entity Framework 从中读取数据。在第一步中,我试图建立数据库连接并在连接时获取状态。但是我收到以下错误;

An exception of type 'System.ArgumentException' occurred in Oracle.ManagedDataAccess.dll but was not handled in user code

我的连接字符串如下;

 <connectionStrings>
    <add name="EBS_UCAS_DbConnection" providerName="Oracle.ManagedDataAccess.Client" connectionString="Data Source=(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 193.00.100.00)(PORT = 1500)))(CONNECT_DATA =(SERVICE_NAME = myservice)));Persist Security Info=True;User ID=myuser;Password=mypassword;Unicode=True"/>
</connectionStrings>

这是我尝试测试代码的地方,我在这里遇到错误

 public void TestOracleConnection()
    {
        using (var dbContext = new UCAS_dbContext())
        {
            var data = dbContext.SyncCodes.Select(x => x.SyncDesc).ToList();

            var xx = "dd";
        }
    }

基础 DBContext class

public class BaseContext<TContext> : DbContext where TContext : DbContext
{
    static BaseContext()
    {
        Database.SetInitializer<TContext>(null);
    }

    protected BaseContext()
    : base("name = EBS_UCAS_DbConnection")
    { }
}

DbContext class

 public class UCAS_dbContext :BaseContext<UCAS_dbContext>
{
    public DbSet<SyncCodesEntity> SyncCodes { get; set; }
}

型号class

 [Table("SYNC_CODES")]
public class SyncCodesEntity
{
    [Key]
    public int ID { get; set; }

    public string SyncDesc { get; set; }
}

在 TNS 工作的其他项目中

 public string database = "(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 193.00.100.00)(PORT = 1500)))(CONNECT_DATA =(SERVICE_NAME = myserver)))";

   public void setConnectString(string username, string password, string database)
    {

        if (this.disposed)
        {
            throw new ObjectDisposedException("clarusOracleObj");
        }
        connectString = "Data Source=" + database + ";Persist Security Info=True;User ID=" + username + ";Password=" + password;
        try
        {
            dbConn = new OracleConnection(connectString);
            dbConn.Open();
            dbConn.Close();
        }
        catch (Exception ex)
        {
            //reset the connectString and throw the exception again.
            connectString = "";
            throw ex;
        }
    }

从 visual studio 中的数据 属性 复制连接字符串,我使用 .NET Framework Data Provider for Oracle

 Data Source=sittingbull.blackpool.ac.uk:1500/myservice;Persist Security Info=True;User ID=myuser;Password=***********;Unicode=True

一个问题可能是您的 TNS 中的空格。用双引号括起来,即试试这个:

<connectionStrings>
    <add 
       name="EBS_UCAS_DbConnection" 
       providerName="Oracle.ManagedDataAccess.Client" 
       connectionString='"Data Source=(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 193.00.100.00)(PORT = 1500)))(CONNECT_DATA =(SERVICE_NAME = myservice)))";Persist Security Info=True;User ID=myuser;Password=mypassword;Unicode=True'/>
</connectionStrings>

<connectionStrings>
    <add 
       name="EBS_UCAS_DbConnection" 
       providerName="Oracle.ManagedDataAccess.Client" 
       connectionString="&quot;Data Source=(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 193.00.100.00)(PORT = 1500)))(CONNECT_DATA =(SERVICE_NAME = myservice)))&quot;;Persist Security Info=True;User ID=myuser;Password=mypassword;Unicode=True"/>
</connectionStrings>

经过长时间的斗争找到了我的问题的答案。

因为我在 WPF 应用程序中使用,所以在 App.Config 文件中定义数据源别名和 TNS 详细信息到您尝试读取的 oracle 数据库;

App.Config

<oracle.manageddataaccess.client>
 <version number="*">
  <dataSources>
    <dataSource alias="DefaultDataSource" descriptor="(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 190.00.100.00)(PORT = 1500)))(CONNECT_DATA =(SERVICE_NAME = myserver)))"/>
  </dataSources>
 </version>
</oracle.manageddataaccess.client>

并用用户名和密码指向连接字符串中的数据源别名

<connectionStrings>
  <add name="OracleDbContext" providerName="Oracle.ManagedDataAccess.Client" connectionString="Data Source=DefaultDataSource;User ID=myusername;Password=mypass;"/>
</connectionStrings>

现在您可以将 OracleDbContext 与 DbContext class 结合使用来引用此数据库;

在执行此实现时,我遇到了另一个错误

求解System.Data.Entity.Core.EntityCommandExecutionException

System.Data.Entity.Core.EntityCommandExecutionException' with InnerException: ORA-00942 table or view does not exist

这个错误的发生是由于默认模式选择错误,我相信它选择了 dbo,在我的情况下模式是 ucas,所以我需要在 onModelCreating 覆盖方法中定义模式

 public class UCAS_dbContext :BaseContext<UCAS_dbContext>
{

    public DbSet<SyncCodesEntity> SyncCodes { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("UCAS");
    }
}

最后是我的测试连接 class

测试连接

 public void TestOracleConnection()
    {
        using (var dbContext = new UCAS_dbContext())
        {
            var query = (from b in dbContext.SyncCodes
                         select b).ToList();

        }
    }