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.
I have a Db class, and in the constructor method i used the method called
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. Also i'm doing these in OnAppearing()
method to show my ListView items on the startup, but i face this exception error:
Microsoft.Data.Sqlite.SqliteExceptions: 'SQLite Error 1: 'no such table:Workers'.'
(see the image below)
That it seems the Database.EnsureCreated()
is not creating my database. 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:
And this is my 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 :
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.
(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).
Now the code looks like this:
using (Db db = new Db(App.db_path))
{
List<Worker> AllWorkers = db.Workers.ToList();
WorkersListView.ItemsSource = AllWorkers;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.