简体   繁体   English

Database.EnsureCreated似乎不是在创建数据库(xamarin.forms)

[英]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. 我正在使用EntityFrameworkSqlite作为数据库制作一个简单的应用程序,以将该人添加并保存到数据库中并将其添加到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.

相关问题 如何在Database.EnsureCreated()和EF Xamarin.Forms之后使用Database.Migration() - How to use Database.Migration() after Database.EnsureCreated() EF Xamarin.Forms Database.EnsureCreated无法正常工作 - Database.EnsureCreated not working 我应该把Database.EnsureCreated放在哪里? - Where should I put Database.EnsureCreated? EF Core Database.EnsureCreated 获取创建表 SQL 并且不向数据库发送请求 - EF Core Database.EnsureCreated get creating table SQL and without sending request to database EntityFramework Core Database.EnsureCreated不会创建数据库 - EntityFramework Core Database.EnsureCreated doesn't create database Xamarin.Forms MVVM ViewModel 如何与数据库交互? - Xamarin.Forms MVVM How does the ViewModel Interact with the Database? Database.EnsureCreated EF 7 ASP.NET 5 MVC 6处的堆栈溢出异常 - Stack Overflow exception at Database.EnsureCreated EF 7 ASP.NET 5 MVC 6 如何使用 .NET 6 在 aspnet 核心 Web 应用程序中执行 database.ensurecreated()? - How do you do database.ensurecreated() in aspnet core web application using .NET 6? 数据库 sqlite 中的项目未更新 (Xamarin.Forms) - Items in database sqlite are not updating (Xamarin.Forms) Xamarin.forms Web服务到Azure数据库 - Xamarin.forms webservice to azure database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM