我正在尝试让EF迁移在Xamarin Forms项目中工作。我有一个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设置Core项目?但是,这将如何工作?最后,我需要进行迁移才能在电话中工作。
Xamarin.EssentialsFileSystem.AppDataDirectory
在您要创建迁移的Windows / Mac上不起作用。
使用此sqlite数据库路径创建迁移:
optionsBuilder.UseSqlite($"Data Source=migrations.db3");
此路径是相对路径,最终将migrations.db3
在项目文件夹中创建一个文件。只是ignote .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] 删除。
我来说两句