zoukankan      html  css  js  c++  java
  • 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("");
            }
        }
  • 相关阅读:
    MyEclipse Tern was unable to complete your request in time
    12.5.2 访问被覆盖的方法
    猎头、培训与咨询的价值(2)【补1】——北漂18年(93)
    12.5.1 通过 @ISA 继承
    从柯洁对战AlphaGo,看商业智能
    jquery ajax POST 例子详解
    利用组合索引优化
    Oracle range 分区表
    Perl 面向对象上
    JSON导出CSV中文乱码解决方案
  • 原文地址:https://www.cnblogs.com/buchizaodian/p/12818250.html
Copyright © 2011-2022 走看看