UWPでのデータベースといったらSQLiteといった感じです。小規模なら、クラスのインスタンスをそのままJSON.NETでさくっとシリアライズ・デシリアライズでいいですが、データ量がちょっと多くなってくると、いつ終了してもいいように備えないといけないUWPでは、いちいち全部保存というのも頂けない感じになってきます。
ということでDBを使いましょう。
プロジェクトの作成
UWPのプロジェクトを作成しておきまず。
DBアクセス用ライブラリ
SQLiteをいい感じに使えるようにしてくれるライブラリです。正式リリースされてないEntity Framework Core 1.0か、SQLite.NETのPCL版を使うのがいいと思います。ここでは将来性を買って、Entity Framework Core 1.0のRC1を使っていってみようと思います。
NuGetでプレリリースを含めるを選択して以下のパッケージをインストールします。
- EntityFramework.SQLite
- EntityFramework.Commands
RC版の制限事項に依存する設定
.NET Nativeと相性が悪いところが今の段階ではあるみたいで、それを解決するためにおまじないを追加しないといけません。PropertiesのDefault.rd.xmlに対して、以下の1行を追加します。
<Type Name="System.Collections.ArrayList"Dynamic="Required All" />
追加する場所はAdd your application specific runtime directives here.というのが書いてあるしたになります。追加したファイルは以下のようになります。
<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata"><Application><Assembly Name="*Application*"Dynamic="Required All"/><Type Name="System.Collections.ArrayList"Dynamic="Required All" /></Application></Directives>
動作確認
以下のようなクラスを定義します。
using Microsoft.Data.Entity;
namespace App47
{
publicclass SampleContext : DbContext
{
public DbSet<Person> People { get; set; }
protectedoverridevoid OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("sample.db");
}
protectedoverridevoid OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Person>()
.Property(x => x.Name)
.IsRequired();
}
}
publicclass Person
{
publicint Id { get; set; }
publicstring Name { get; set; }
}
}
DBの作成
パッケージマネージャーコンソールで以下のコマンドを実行します。
PM> Add-Migration Sample
To undo this action, use Remove-Migration.
画面
画面を作ります。
<Page xClass="App47.MainPage"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlnsx="http://schemas.microsoft.com/winfx/2006/xaml"xmlnslocal="using:App47"xmlnsd="http://schemas.microsoft.com/expression/blend/2008"xmlnsmc="http://schemas.openxmlformats.org/markup-compatibility/2006"mcIgnorable="d"><Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"><ListView xName="ListView"><ListViewItemTemplate><DataTemplate xDataType="local:Person"><TextBlock Text="{x:Bind Name}" /></DataTemplate></ListViewItemTemplate></ListView></Grid></Page>
コードビハインドで、適当にデータを作って読み込む処理を書きます。
using Microsoft.Data.Entity;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace App47
{
publicsealedpartialclass MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
using (var ctx = new SampleContext())
{
ctx.Database.Migrate();
var people = Enumerable.Range(1, 100).Select(x => new Person { Name = $"tanaka {x}" }).ToArray();
ctx.People.AddRange(people);
ctx.SaveChanges();
}
}
protectedoverridevoid OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
using (var ctx = new SampleContext())
{
this.ListView.ItemsSource = ctx.People.ToArray();
}
}
}
}
実行
実行すると以下のような感じでデータが表示されます。
![f:id:okazuki:20160308220554p:plain f:id:okazuki:20160308220554p:plain]()
まとめ
とりあえずEntity Frameworkを使えば簡単にローカルのSQLiteに繋げることがわかりました。
この調子で使っていこうと思います。