ef core 连oracle

 nuget :Oracle.EntityFrameworkCore

 直接写连接串的方式

此方法实例化DDDContext时无需传入参数

using Microsoft.EntityFrameworkCore;
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {

            using (var context = new DDDContext())
            {
                var Scale = context.Scale.ToList();

                foreach (var s in Scale)
                {
                    Console.WriteLine(s.CODE);
                }
            }   
        }
    }

    public class DDDContext : DbContext
    {
        public DbSet<tablename> Scale { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder options)
            => options.UseOracle(@"Data Source=xxx:1521/orcl; User Id=xxx; password=xxx;Pooling=false;");
    }

    [Table("tablename")]
    public class T_SCALE
    {
        [Key]
        public int ID { get; set; }
   
        public string CODE { get; set; }

        public string NAME { get; set; }
    }
}

读取配置文件的方式

    public class DBContext : DbContext
    {
        public string _config;
        public DBContext()
        {
            var config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
            _config = config["ConnectionStrings:xxx"];
        }

        protected override void OnConfiguring(DbContextOptionsBuilder options)
            => options.UseOracle(_config);

    }

ASP.net core 配置方式

Startup.cs

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            var sqlConnection = Configuration["ConnectionStrings:xxx"];
            services.AddDbContextPool<DBContext_INTF1>(option => option.UseOracle(sqlConnection));
        }

DBContext.cs

    public class DBContext : DbContext
    {
        public DbSet<T_SCALE> Scale { get; set; }


        public DBContext_INTF1(DbContextOptions<DBContext> options) : base(options)
        {

        }
    }

HomeController.cs

    [ApiController]
    [Route("[controller]")]
    public class HomeController : Controller
    {
        DbContextOptions<DBContext_INTF1> _dBContext;

        public HomeController(DbContextOptions<DBContext_INTF1> dBContext)
        {
            _dBContext = dBContext;
        }

        [HttpGet]
        public IActionResult Index()
        {

            using (var context = new DBContext_INTF1(_dBContext))
            {
                
            }
            return Content("");
        }
    }
原文地址:https://www.cnblogs.com/buchizaodian/p/12818250.html