やってみる

アウトプットすべく己を導くためのブログ。その試行錯誤すらたれ流す。

Raspbian stretch Mono で EntityFramework が使えない……

 サポートしてると書いてあるけど、どう使うかわからん。

対象環境

概要

 C#でSQLite3を使いたい。コードファーストで書きたい。EntityFrameworkを使えばできるはず。

調査

公式

As of Mono 2.11.3, Microsoft’s open sourced EntityFramework is part of Mono’s distribution.

Mono 2.11.3以降、MicrosoftオープンソースのEntityFrameworkはMonoの配布の一部です。

 しかし、MonoDevelopを立ち上げてusingコードを書いたが、System.Data名前空間すら存在しなかった……。

 あと、NuGetでEntityFrameworkをインストールしようとすると失敗する。

Package EntityFramework 6.2.0 is not compatible with netstandard1.6 (.NETStandard,Version=v1.6). Package EntityFramework 6.2.0 supports:
  - net40 (.NETFramework,Version=v4.0)
  - net45 (.NETFramework,Version=v4.5)

 entityframeworkのソースコードは公開されているようだが、どうしろと? そもそも.NETStandardだとか.NETFrameworkだとかは何なの? これは別枠で調べよう。

テストコード

 上記のコードを参考にしてみた。

using System;
using System.Data.Entity;

namespace ConsoleEFTest
{
        public class SimpleContext : DbContext
        {
                public SimpleContext () : base("name = SimpleContext")
                {
                }
        }
}

 2行目のusing System.Data.Entity;ですでに存在しないエラー……。私の環境はMono 5.16.0。サポートは2.11.3以降。使えるんじゃなかったの? どうやって使うの? ググっても見つけられなかった……。

プロジェクト

 上記URLで公開してくれてるプロジェクトを入手してみた。

$ git clone https://github.com/csharpguy76/ConsoleEFTest
  1. .sinファイルをダブルクリックしてMonoDevelopを起動する
  2. 実行する
  3. 実行ファイルが生成された

 ビルドまでは成功したようだ。

実行

  1. 実行ファイルに実行権限を付与する
  2. ターミナルにて実行ファイルを実行する
  3. 数十秒待機すると、以下エラーが表示された
$ /tmp/work/ConsoleEFTest/ConsoleEFTest/bin/Debug/ConsoleEFTest.exe

Unhandled Exception:
System.Data.Entity.Core.ProviderIncompatibleException: An error occurred accessing the database. This usually means that the connection to the database failed. Check that the connection string is correct and that the appropriate DbContext constructor is being used to specify it or find it in the application's config file. See http://go.microsoft.com/fwlink/?LinkId=386386 for information on DbContext and connections. See the inner exception for details of the failure. ---> System.Data.Entity.Core.ProviderIncompatibleException: The provider did not return a ProviderManifestToken string. ---> System.Data.SqlClient.SqlException: Snix_Connect (provider: SNI_PN7, error: 40 - SNI_ERROR_40)
Snix_Connect (provider: SNI_PN7, error: 40 - SNI_ERROR_40)
...

 長すぎるので省略。参考元をみると、以下のRDBMSを使っているらしい。それをインストールしていないからエラーになったのかな?

所感

 App.configでSQLite3に接続するようにすればいいのかな? その前に.NETをとりまくソフトウェア環境が複雑化しているようなので調べてみる必要がありそう。