やっとできた!
成果物
前回
- http://ytyaru.hatenablog.com/entry/2020/02/10/000000
- http://ytyaru.hatenablog.com/entry/2020/02/11/000000
- http://ytyaru.hatenablog.com/entry/2020/02/12/000000
- http://ytyaru.hatenablog.com/entry/2020/02/13/000000
- http://ytyaru.hatenablog.com/entry/2020/02/14/000000
- http://ytyaru.hatenablog.com/entry/2020/02/15/000000
対象環境
- Raspbierry pi 3 Model B+
- Raspbian stretch(9.0) 2018-06-27
- Mono 5.16.0
- MonoDevelop 7.6 build 711
- Eto.Forms 2.4.1 拡張機能, NuGetパッケージ
- .NET Core 2.2, MonoDevelop参照方法
参考
以下のコマンドでテーブル作成するらしい。
$ dotnet ef database update
手順
事前に.NET Core 2.2.101をインストールしておくこと。
1. .NET Core コンソールアプリ プロジェクト作成
ターミナルを起動し、以下のコマンドを実行する。
mkdir -p /tmp/work/DotNetProjects/HelloCoreEFCore cd /tmp/work/DotNetProjects/HelloCoreEFCore dotnet new console dotnet restore dotnet publish -r linux-arm /tmp/work/DotNetProjects/HelloCoreEFCore/bin/Debug/netcoreapp2.2/linux-arm/HelloCoreEFCore
2. EFCore一式入手
dotnet add package Microsoft.EntityFrameworkCore.Tools.DotNet dotnet add package Microsoft.EntityFrameworkCore dotnet add package Microsoft.EntityFrameworkCore.Design dotnet add package Microsoft.EntityFrameworkCore.Tools dotnet add package Microsoft.EntityFrameworkCore.Sqlite
3. ソースコード作成
Program.cs
using System;
namespace HelloCoreEFCore
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World! and EFCore.");
using (var db = new AppDbContext()) {
var account = new Accounts() { Id=0, GitHubId=0, Username="user1", Password="pass1" };
db.Accounts.Add(account);
db.SaveChanges();
Console.WriteLine(account);
}
}
}
}
AppDbContext.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace HelloCoreEFCore
{
public class AppDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseSqlite(@"Data Source='/tmp/work/hello.db'");
}
public DbSet<Accounts> Accounts { get; set; }
}
}
Accounts.cs
using System;
namespace HelloCoreEFCore
{
public class Accounts
{
public int Id { get; set; }
public int GitHubId { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
}
4. ビルド
dotnet restore dotnet publish -r linux-arm
5. DB作成
dotnet ef migrations add FirstMigration_CreateTable dotnet ef database update
6. DBファイル内容確認
sqlite3 /tmp/work/hello.db ".tables" sqlite3 /tmp/work/hello.db ".schema" sqlite3 /tmp/work/hello.db "select * from Accounts;"
.tables
$ sqlite3 /tmp/work/hello.db ".tables" Accounts __EFMigrationsHistory
.schema
$ sqlite3 /tmp/work/hello.db ".schema"
CREATE TABLE IF NOT EXISTS "__EFMigrationsHistory" (
"MigrationId" TEXT NOT NULL CONSTRAINT "PK___EFMigrationsHistory" PRIMARY KEY,
"ProductVersion" TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS "Accounts" (
"Id" INTEGER NOT NULL CONSTRAINT "PK_Accounts" PRIMARY KEY AUTOINCREMENT,
"GitHubId" INTEGER NOT NULL,
"Username" TEXT NULL,
"Password" TEXT NULL
);
CREATE TABLE sqlite_sequence(name,seq);
select
$ sqlite3 /tmp/work/hello.db "select * from Accounts;" 1|0|user1|pass1
所感
できたっっっっっ(泣) やっと、ついに……長かった、辛かった。
dotnet ef database updateコマンドの実行が必要だった。ただそれだけのことだった。
いやだって、そもそもコマンドが必要とか、コマンドでビルドするとか、聞いてないし。IDEたるMonoDevelopが全部よしなにやってくれるべきだろ。エラーばかり吐く役立たずじゃん。なにが統合開発環境だよ。お前の出番ないじゃん。全部コマンドでやらないと成功しないじゃん。それとも何か設定できる方法があるのだろうか。
なさそう。