gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:18:56 31 4
gpt4 key购买 nike

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

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

最佳答案

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

因为我在 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();

}
}

关于c# - 在 C# 中使用 Entity Framework 读取 Oracle 数据连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40543865/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com