3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

.NET CoreからEntity Framework Coreを経由してOracle Autonomous Databaseへ接続

Last updated at Posted at 2020-01-08
1 / 2

概要

.net からOracle Autonomous Databaseへ接続してみました。
(Oracle Autonomous Databaseには、Autonomous Data Warehouse(ADW)とAutonomous Transaction Processing(ATP)の二種類がありますが、接続方法は同じです。)

1 準備

1.1 Autonomous Transaction Processingデータベース作成

image.png

1.2 クライアントの資格証明(ウォレット)のダウンロード

image.png
image.png

ウォレットファイルを保存します。

1.3 テスト用テーブルの作成

1.3.1 テーブルBlogsの作成(列BlogIdとUrlが存在します)

image.png

2 接続テスト

2.1 Visual Studio 起動、プロジェクトAtptest1の作成 (.netCore console application)

image.png

2.2 NugetからOracle.EntityFrameworkCore最新版をインストール

image.png
image.png

2.3 ウォレットファイル解凍

上記1.2 でダウンロードしたウォレットファイルをAtptest1のbinフォルダーに解凍します。

※任意のフォルダーに解凍して問題ありませんが、下記を注意する必要があります。

  • tnsnames.oraとsqlnet.oraはexeと同じフォルダーに保存する必要がある
    image.png

2.4 sqlnet.ora編集

上記2.3で解凍されたファイルの中のsqlnet.oraを環境にあわせてDIRECTORY部分を修正します。(相対パス、絶対パスどちらでも問題ありません)
image.png

WALLET_LOCATION = (SOURCE = (METHOD = file) (METHOD_DATA = (DIRECTORY="./")))
SSL_SERVER_DN_MATCH=yes

2.5 簡単なコードでOracle ATP接続動作確認

2.5.1 サンプルコード

using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

namespace Atptest1
{
    class Program
    {
        public class BloggingContext : DbContext
        {
            public DbSet<Blog> Blogs { get; set; }

            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                optionsBuilder.UseOracle(@"User Id=ADMIN;Password=Welcome!123456;Data Source=db201912241112_medium");
               //  User Id=<your username> 
               //  例 User Id=ADMIN
               //  Password=<your password> 
               //  例 Password=Welcome!123456
               //  Data Source=<your servicename>
               //  例 your servicename=db201912241112_medium
            }
        }

        public class Blog
        {
            public int BlogId { get; set; }
            public string Url { get; set; }
        }

        static void Main(string[] args)
        {
            using (var db = new BloggingContext())

            {
                var blog = new Blog { Url = "https://blogs.oracle.com" };
                db.Blogs.Add(blog);
                db.SaveChanges();

            }

            using (var db = new BloggingContext())
            {
                var blogs = db.Blogs;
            }
        }
    }
}

※「コード中の接続文字列(ID、パスワード、データソース)は、環境に合わせて変更してください。」

→サービス名については、下記のマニュアルをご参照ください。
https://docs.oracle.com/cd/E83857_01/paas/atp-cloud/atpug/connect-predefined.html

2.5.2 実行結果

image.png
image.png
無事に接続して、Entity Framework Coreを経由してデータをATPに挿入できました!

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?