LoginSignup
0
2

More than 5 years have passed since last update.

Xamarin で Entity Framework Core を利用するときの注意事項メモ

Posted at

Xamarin で Entity Framework Core を利用するときの注意事項メモ

はじめに

Xamarin で Entity Framework Core を利用するときに、いくつかはまった事があったので、対応策のメモを記載しておきます。

Migration Tool

Xamarin のプロジェクトに対して、Entity Framework Core のマイグレーションツールの dotnet ef コマンドを直接実行することができません。コンソールアプリケーションに Xamarin のプロジェクトを参照させて、コマンドを実行する必要があります。

.NET Core のコンソールアプリケーションをソリューションに追加し、コンソールアプリケーションの参照設定で、Xamarin のプロジェクトを追加します。

ConsoleApp

コンソールアプリケーションのプロジェクトのディレクトリで、dotnet ef コマンドを実行してください。

dotnet-ef-command

dotnet ef migrations add [マイグレーションの名前] -p [DbContext を持つプロジェクトの csproj ファイル]

そうすると、マイグレーション関連のコードを生成することができます。

dotnet-ef-command

System.Buffuers 関連のビルドエラー

Android プロジェクトをビルドすると、以下のようなビルドエラーがでることがあります。

Can not resolve reference: System.Buffers, referenced by System.Memory. Please add a NuGet package or assembly reference for System.Buffers, or remove the reference to System.Memory.

この場合は、System.Buffers を NuGet を使ってインストールすると解決できます。

Linker の設定

Release ビルド時に、リンカーにより、使用 (または参照) されないアセンブリ、型、メンバーが破棄されので、Entity Framework Core が利用しているクラス類が破棄されないように、Linker の設定を行う必要があります。以下のような xml ファイルを iOS, Andorod のプロジェクトに追加して、ビルドアクションの設定を行う必要があります。

<?xml version="1.0" encoding="utf-8" ?>
<linker>
  <assembly fullname="mscorlib">
    <type fullname="System.String">
      <method name="Compare"></method>
      <method name="CompareTo"></method>
      <method name="ToUpper"></method>
      <method name="ToLower"></method>
    </type>
  </assembly>
  <assembly fullname="System.Core">
    <type fullname="System.Linq.Expressions.Expression`1"></type>
    <type fullname="System.Linq.Queryable"></type>
  </assembly>
</linker>

Visual Studio のビルドアクションで、[LinkDescription] の設定を行ってください。

ConsoleApp

SQLitePCL.Batteries_V2.Init() の呼び出し

SQLite データベースを利用している場合は、アプリケーションの初期化時に、SQLitePCL.Batteries_V2.Init() を呼び出す必要があります。

SQLitePCL.Batteries_V2.Init();

さいごに

GitHub の Issue に Entity Framework Core がうまく動かないときの回避策がまとめられています。こちらも定期的にウオッチしておくと良いかもしれません。

0
2
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
0
2