2

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

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 类

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

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

DbContext 类

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

模型类

 [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
4

2 回答 2

2

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

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

应用程序配置

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

所以现在你可以使用带有 DbContext 类的 OracleDbContext 来引用这个数据库;

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

解决 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");
    }
}

最后是我的测试连接类

测试连接

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

        }
    }
于 2016-11-11T11:21:51.533 回答
0

一个问题可能是 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>
于 2016-11-11T09:32:30.593 回答