You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Xamarin Forms应用连接至远程Oracle 12c Release 2数据库?

Fixing Oracle 12.2 Connectivity in Xamarin PCL (System.Drawing & EF6 Compatibility Issues)

Hey there, let's work through this Oracle connectivity headache with your Xamarin PCL project. The System.Drawing and EF6 compatibility issues you're hitting are super common when mixing old .NET Framework-focused libraries with Xamarin's cross-platform model—here's how to fix it:

1. Ditch EF6 for EF Core (If You Need an ORM)

EF6 is built for the full .NET Framework and doesn't play nice with Xamarin PCLs. Instead, switch to Entity Framework Core—it's designed for cross-platform use, including Xamarin.

  • Install these NuGet packages (make sure versions match):
    • Microsoft.EntityFrameworkCore (pick a stable version like 6.0 or 7.0)
    • Oracle.EntityFrameworkCore (Oracle's official EF Core provider, match its version to your EF Core version)
  • This eliminates the EF6-specific compatibility baggage that's causing conflicts.

2. Remove All System.Drawing Dependencies

System.Drawing is a Windows desktop-only library—Xamarin PCLs have zero support for it. You need to cut this dependency entirely:

  • Scan your C# code for using System.Drawing; statements and delete them.
  • If you were using System.Drawing for image handling, replace it with cross-platform alternatives like SkiaSharp (a popular open-source image library for Xamarin) or Xamarin.Forms' built-in Image controls.
  • If System.Drawing is being pulled in indirectly by an old Oracle driver, that means you're using the wrong driver (see step 3).

3. Use Oracle's Cross-Platform Driver

Forget the old ODP.NET Framework driver—use Oracle.ManagedDataAccess.Core, Oracle's official cross-platform driver that supports Xamarin, .NET Core, and more.

  • Install the NuGet package: Oracle.ManagedDataAccess.Core
  • Here's a simple example of raw database access (no ORM) to test connectivity:
    using Oracle.ManagedDataAccess.Client;
    using System.Threading.Tasks;
    
    public async Task FetchOracleData()
    {
        // Replace with your actual DB credentials and server details
        string connString = "User Id=your_user;Password=your_pass;Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=your_server_ip)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=your_service_name)))";
        
        using (var connection = new OracleConnection(connString))
        {
            await connection.OpenAsync();
            using (var command = new OracleCommand("SELECT * FROM YOUR_TABLE_NAME", connection))
            {
                using (var reader = await command.ExecuteReaderAsync())
                {
                    while (await reader.ReadAsync())
                    {
                        // Process your data here
                        int id = reader.GetInt32(0);
                        string name = reader.GetString(1);
                    }
                }
            }
        }
    }
    

PCLs have strict API limitations that often cause compatibility issues. Consider migrating your data access layer to a .NET Standard library—it's the modern cross-platform library format, fully supported by Xamarin, and has broader API support than PCLs.

  • In Visual Studio, you can create a new .NET Standard library, copy your PCL code over, and reference it from your Xamarin projects.

5. Fix NuGet Package Conflicts

If you're still seeing assembly mismatches:

  • Open the NuGet Package Manager for your solution, and ensure all packages are on compatible versions (no mixed major versions, e.g., EF Core 6 with Oracle.EFCore 7).
  • Clear your NuGet cache: Go to Tools > NuGet Package Manager > Package Manager Settings > Clear All NuGet Caches, then reinstall all packages.

Final Check

Make sure your Xamarin projects are targeting stable, up-to-date framework versions (e.g., Xamarin.Android 13.0, Xamarin.iOS 16.0)—older versions have more compatibility gaps with modern libraries.

内容的提问来源于stack exchange,提问作者Tirth21896

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:55:14