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