Entity framework Xamarin表单:实体框架核心的重新创建无效
这是我的密码:Entity framework Xamarin表单:实体框架核心的重新创建无效,entity-framework,xamarin,xamarin.forms,xamarin.android,entity-framework-core,Entity Framework,Xamarin,Xamarin.forms,Xamarin.android,Entity Framework Core,这是我的密码: public class ApplicationContext : DbContext { private const string databaseName = "Database.db"; public DbSet<Setting> Setting { get; set; } protected override void OnConfiguring(DbContextOp
public class ApplicationContext : DbContext
{
private const string databaseName = "Database.db";
public DbSet<Setting> Setting { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
String databasePath = "";
switch (Device.RuntimePlatform)
{
case Device.iOS:
SQLitePCL.Batteries_V2.Init();
databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "..", "Library", databaseName); ;
break;
case Device.Android:
databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), databaseName);
break;
case Device.WPF: {
databasePath = Path.Combine(Environment.CurrentDirectory, databaseName);
break;
}
default:
throw new NotImplementedException("Platform not supported");
}
if (!File.Exists(databasePath))
{
File.Create(databasePath);
}
optionsBuilder.UseSqlite($"Filename={databasePath}");
}
}
public class Setting
{
[Key]
public string Key { get; set; }
public string Value { get; set; }
}
公共类应用程序上下文:DbContext
{
private const string databaseName=“Database.db”;
公共数据库集设置{get;set;}
配置时受保护的覆盖无效(DBContextOptions Builder Options Builder)
{
字符串databasePath=“”;
交换机(设备运行时平台)
{
case Device.iOS:
SQLitePCL.batters_V2.Init();
databasePath=Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),“.”、“Library”、databaseName);
打破
case设备。Android:
databasePath=Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal),databaseName);
打破
case Device.WPF:{
databasePath=Path.Combine(Environment.CurrentDirectory,databaseName);
打破
}
违约:
抛出新的NotImplementedException(“不支持平台”);
}
如果(!File.Exists(databasePath))
{
创建(数据库路径);
}
optionsBuilder.UseSqlite($“Filename={databasePath}”);
}
}
公共班级设置
{
[关键]
公共字符串密钥{get;set;}
公共字符串值{get;set;}
}
在WPF中,一切都运行良好
然而,在Android中,即使我以前使用了并重新创建了,它也总是报告“没有这样的表”错误。
这个怎么了?我怎样才能解决它?谢谢。请确保已安装正确的nuget软件包:
我用您的代码编写了一个演示,在我这方面效果很好,以下是我的代码:
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
ApplicationContext context = new ApplicationContext();
context.Database.EnsureCreated();
Task.Run (async () => {
var newPosts = new Setting() { Key = "123", Value = "test" };
context.Setting.Add(newPosts);
var result = context.Setting.Where(X => X.Key == "123").FirstOrDefault();
Console.WriteLine(result.Value);
} );
}
}
public class ApplicationContext : DbContext
{
private const string databaseName = "Database.db";
public DbSet<Setting> Setting { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
String databasePath = "";
switch (Device.RuntimePlatform)
{
case Device.iOS:
SQLitePCL.Batteries_V2.Init();
databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "..", "Library", databaseName); ;
break;
case Device.Android:
databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), databaseName);
break;
case Device.WPF:
{
databasePath = Path.Combine(Environment.CurrentDirectory, databaseName);
break;
}
default:
throw new NotImplementedException("Platform not supported");
}
if (!File.Exists(databasePath))
{
File.Create(databasePath);
}
optionsBuilder.UseSqlite($"Filename={databasePath}");
}
}
public class Setting
{
[Key]
public string Key { get; set; }
public string Value { get; set; }
}
public分部类主页面:ContentPage
{
公共主页()
{
初始化组件();
ApplicationContext上下文=新的ApplicationContext();
context.Database.recreated();
Task.Run(异步()=>{
var newPosts=newsetting(){Key=“123”,Value=“test”};
context.Setting.Add(newPosts);
var result=context.Setting.Where(X=>X.Key==“123”).FirstOrDefault();
Console.WriteLine(result.Value);
} );
}
}
公共类应用程序上下文:DbContext
{
private const string databaseName=“Database.db”;
公共数据库集设置{get;set;}
配置时受保护的覆盖无效(DBContextOptions Builder Options Builder)
{
字符串databasePath=“”;
交换机(设备运行时平台)
{
case Device.iOS:
SQLitePCL.batters_V2.Init();
databasePath=Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),“.”、“Library”、databaseName);
打破
case设备。Android:
databasePath=Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal),databaseName);
打破
case Device.WPF:
{
databasePath=Path.Combine(Environment.CurrentDirectory,databaseName);
打破
}
违约:
抛出新的NotImplementedException(“不支持平台”);
}
如果(!File.Exists(databasePath))
{
创建(数据库路径);
}
optionsBuilder.UseSqlite($“Filename={databasePath}”);
}
}
公共班级设置
{
[关键]
公共字符串密钥{get;set;}
公共字符串值{get;set;}
}
我上传了我的测试项目,随时可以问我任何问题。谢谢你为我写了一个样本。我在任务中添加了一个try&catch。运行时,它会报告一个错误:{Microsoft.EntityFrameworkCore.DbUpdateException:更新条目时出错。有关详细信息,请参阅内部异常。-->Microsoft.Data.Sqlite.SqliteException:Sqlite错误19:'UNIQUE constraint failed:Setting.Key'。我尝试的只是Github中的示例项目,而不是创建一个全新的项目。此外,我还介绍了使用最新版本调用了上面的所有NuGet软件包(您提供的部分软件包不是最新版本)。我更新了示例项目,请重新测试。我在Task.Run()中添加新数据时更改了代码。非常感谢。我再次添加了try&catch,发现var结果返回一个null对象,而它应该返回刚刚添加的对象。我在其中添加了一个“context.SaveChanges();”,它现在可以正常工作了。