我有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";
}
}
public class BaseContext<TContext> : DbContext where TContext : DbContext
{
static BaseContext()
{
Database.SetInitializer<TContext>(null);
}
protected BaseContext()
: base("name = EBS_UCAS_DbConnection")
{ }
}
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; }
}
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
答案 0 :(得分: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>
所以现在你可以使用OracleDbContext和DbContext类来引用这个数据库;
在执行此实现时,我遇到了另一个错误
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");
}
}
最后我的测试Connection类
public void TestOracleConnection()
{
using (var dbContext = new UCAS_dbContext())
{
var query = (from b in dbContext.SyncCodes
select b).ToList();
}
}
答案 1 :(得分: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=""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>