XamarinFormsプロジェクトでEF移行を機能させようとしています。SQLiteデータベースのDbContextがあります。
public class TestDbContext : DbContext
{
public DbSet<Item> Items { get; set; }
public TestDbContext()
{
SQLitePCL.Batteries_V2.Init();
this.Database.EnsureCreated();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string dbPath = Path.Combine(FileSystem.AppDataDirectory, "testDb.db3");
optionsBuilder
.UseSqlite($"Filename={dbPath}");
}
}
Microsoft.EntityFrameworkCore.Tools
標準ライブラリのnugetをインストールしましたが、機能しませんでした。始まって、この、私は追加ダミー.NETのコアプロジェクトを。
Microsoft.EntityFrameworkCore.Sqlite
とMicrosoft.EntityFrameworkCore.Tools
同様にダミーのプロジェクトにnugetパッケージをIDesignTimeDbContextFactory
ように.net標準ライブラリにを実装しました: public class DesignTimeFactory : IDesignTimeDbContextFactory<TestDbContext>
{
public TestDbContext CreateDbContext(string[] args)
{
return new TestDbContext();
}
}
コマンドを実行するPM> Add-Migration InitialCreate -P XamarinEFTest -S EFDummy
と、例外が発生します。
Build started...
Build succeeded.
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> Xamarin.Essentials.NotImplementedInReferenceAssemblyException: This functionality is not implemented in the portable version of this assembly. You should reference the NuGet package from your main application project in order to reference the platform-specific implementation.
at Xamarin.Essentials.FileSystem.get_PlatformAppDataDirectory()
at Xamarin.Essentials.FileSystem.get_AppDataDirectory()
at XamarinEFTest.TestDbContext.OnConfiguring(DbContextOptionsBuilder optionsBuilder) in C:\Users\szabk\source\repos\XamarinEFTest\XamarinEFTest\XamarinEFTest\TestDbContext.cs:line 24
at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()
at Microsoft.EntityFrameworkCore.Infrastructure.Internal.InfrastructureExtensions.GetService[TService](IInfrastructure`1 accessor)
at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.get_Dependencies()
at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureCreated()
at XamarinEFTest.TestDbContext..ctor() in C:\Users\szabk\source\repos\XamarinEFTest\XamarinEFTest\XamarinEFTest\TestDbContext.cs:line 19
at XamarinEFTest.DesignTimeFactory.CreateDbContext(String[] args) in C:\Users\szabk\source\repos\XamarinEFTest\XamarinEFTest\XamarinEFTest\DesignTimeFactory.cs:line 12
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContextFromFactory(Type factory, Type contextType)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass16_0.<FindContextFactory>b__1()
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Exception has been thrown by the target of an invocation.
VS 2019 16.8.4を使用しています。スライダーメニューを使用して、レイアウトテンプレートからテストxamarinプロジェクトを作成しました。
私は何をする必要がありますか?
編集:たぶん私は別のSQLiteドライバーと別のDesignTimeFactoryでコアプロジェクトをセットアップする必要がありますか?しかし、これはどのように機能しますか?最終的に電話で機能するように移行が必要です。
Xamarin.EssentialsFileSystem.AppDataDirectory
は、移行を作成しようとしているWindows / Macでは機能しません。
移行を作成するには、次のsqliteデータベースパスを使用します。
optionsBuilder.UseSqlite($"Data Source=migrations.db3");
このパスは相対パスmigrations.db3
であり、プロジェクトフォルダーにファイルが作成されます。.dbファイルに注意してください。唯一の目的は、移行を作成することでした。
編集:私は自分のアプリでこのDbContextを使用してきましたが、問題なく動作します。
public class MyDbContext : DbContext
{
private string _databasePath;
public DbSet<MyEntity> Issues { get; set; }
[Obsolete("Don't use this for production. This is only for creating migrations.")]
public MyDbContext() : this("nothing.db")
{
}
public MyDbContext(string dbPath)
{
_databasePath = dbPath;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlite($"Data Source={_databasePath}");
}
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加