[英]Database.EnsureCreated does not seem that it's creating a database (xamarin.forms)
I'm making a simple app using EntityFramework and Sqlite as database to add and save the person into the data base and add it to a ListView, or delete it. 我正在使用EntityFramework和Sqlite作为数据库制作一个简单的应用程序,以将该人添加并保存到数据库中并将其添加到ListView或删除它。
I have a Db class, and in the constructor method i used the method called 我有一个Db类,在构造方法中,我使用了称为
Database.EnsureCreated()
to make my database. 建立我的数据库 And in the MainPage.xaml.cs i made an instance of the Db class(db) and i wanted to get all the Workers table in my database and pass it as the ItemSource of a ListView.
在MainPage.xaml.cs中,我创建了Db class(db)的实例,我想获取数据库中的所有Workers表并将其作为ListView的ItemSource传递。 Also i'm doing these in
OnAppearing()
method to show my ListView items on the startup, but i face this exception error: 我也在
OnAppearing()
方法中执行这些操作,以在启动时显示我的ListView项目,但我遇到此异常错误:
Microsoft.Data.Sqlite.SqliteExceptions: 'SQLite Error 1: 'no such table:Workers'.'
Microsoft.Data.Sqlite.SqliteExceptions:'SQLite错误1:'没有这样的表:Workers'。
(see the image below) (请参见下图)
enter image description here 在此处输入图片说明
That it seems the Database.EnsureCreated()
is not creating my database. 看来
Database.EnsureCreated()
并未创建我的数据库。 or maybe it creates my database but it can't create the tables for some reasons and i want to know those reasons 也许它创建了我的数据库,但是由于某些原因而无法创建表,我想知道那些原因
And here's my Db class: 这是我的Db类:
enter image description here 在此处输入图片说明
And this is my db_path: Android : 这是我的db_path: Android :
string FileName = "Workers.sqlite";
string FileLocation = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string db_path = Path.Combine(FileLocation, FileName);
And the iOS : 和iOS :
string FileName = "Workers.sqlite";
string FileLocation = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "..", "Library");
string db_path = Path.Combine(FileLocation, FileName);
So what is the problem with the database and table creation? 那么数据库和表创建有什么问题?
I just added using
keyword to those lines and it fixed it unexpectedly. 我只是在这些行中添加了
using
关键字,但它意外地修复了它。
(I also removed some of those db_path variables and made the db_path source cleaner and more fluent by getting it from the App class). (我还删除了一些db_path变量,并通过从App类中获取了它,使db_path源代码更干净,更流畅)。
Now the code looks like this: 现在,代码如下所示:
using (Db db = new Db(App.db_path))
{
List<Worker> AllWorkers = db.Workers.ToList();
WorkersListView.ItemsSource = AllWorkers;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.