Search code examples
c#oracle-databaseentity-framework-coreoracle-autonomous-db

Console App updates database but will not connect to data source when querying


I have created a console app to connect to an Oracle ADW database. When I did the initial migration, it created the tables exactly as I expected. Now when I try to query a table on that database, I get the error: "ORA-12154: TNS:could not resolve the connect identifier specified"

I know it can connect as the update-database succeeded. Here is my context:

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WCONSftpFileUploadChecks.Models;

namespace WCONSftpFileUploadChecks.Data
{
    public class OracleDBContext : DbContext
    {
        public OracleDBContext() : base()
        {
        }

        public virtual DbSet<FileDet> FileDetails { get; set; }
        public virtual DbSet<RemotePath> RemotePaths { get; set; }
        public virtual DbSet<Peter> Peters { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseOracle(@"User Id=<USER>;Password=<PASSWORD>;Data Source=<SOURCENAME>");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<FileDet>()
                .Property(p => p.value)
                .HasColumnType("decimal(18,4)");
        }
    }
}

Here is the project structure:

Project Structure

And here is how I am trying to retrieve the data:

using WCONSftpFileUploadChecks.Code;
using WCONSftpFileUploadChecks.Data;
using WCONSftpFileUploadChecks.Models;

namespace WCONSftpFileUploadChecks
{
    public class Program
    {
        public static void Main(string[] args)
        {
            using(var ctx = new OracleDBContext())
            {
                foreach (RemotePath p in ctx.RemotePaths)
                {
                    Console.WriteLine(p.remote_path);
                }

            }
            Console.ReadLine();
            return;
        }
    }
}

And here is where it throws the error:

Error Thrown here

I deliberately haven't included the Model as I don't believe it's relevant. I just cannot see what is wrong.


Solution

  • The answer was straightforward, just two lines added to OnModelCreating method in my context file:

    OracleConfiguration.TnsAdmin = @"C:\path\to\wallet\file";
    
    OracleConfiguration.WalletLocation= @"C:\path\to\wallet\file";