[英]Xamarin.Forms SQLite exception - don't know about PartsManagement.Models.Part[] (<- a custom class)
I am sorry for this re-post of a question, but the original was never answered in a way that I could understand and leverage.对于这个问题的重新发布,我感到很抱歉,但原始问题从未以我可以理解和利用的方式得到回答。 I was not certain if I should try to revive that question or spawn a new one.
我不确定是否应该尝试重新提出这个问题或产生一个新问题。
I am attempting to add a Part object to my Part table within the database I created.我正在尝试将零件 object 添加到我创建的数据库中的零件表中。 I get the following error during execution
我在执行过程中收到以下错误
System.AggregateException
Message=One or more errors occurred. (Don't know about PartsManagement.Models.Part[])
Source=mscorlib
StackTrace:
at System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) [0x00011] in /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2027
at System.Threading.Tasks.Task.Wait (System.Int32 millisecondsTimeout, System.Threading.CancellationToken cancellationToken) [0x00043] in /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2759
at System.Threading.Tasks.Task.Wait () [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2625
at PartsManagement.Database..ctor (System.String dbPath) [0x00027] in C:\Users\5w3rv0\source\repos\PartsManagement\PartsManagement\PartsManagement\Database.cs:18
at PartsManagement.App.get_Database () [0x0000e] in C:\Users\5w3rv0\source\repos\PartsManagement\PartsManagement\PartsManagement\App.xaml.cs:20
at PartsManagement.Views.NewPartPage.Save_Clicked (System.Object sender, System.EventArgs e) [0x00057] in C:\Users\5w3rv0\source\repos\PartsManagement\PartsManagement\PartsManagement\Views\NewPartPage.xaml.cs:25
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021
at Android.App.SyncContext+<>c__DisplayClass2_0.<Post>b__0 () [0x00000] in <4ccdb3137d974856b786e1aeebbfbab6>:0
at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <4ccdb3137d974856b786e1aeebbfbab6>:0
at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in <4ccdb3137d974856b786e1aeebbfbab6>:0
at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.71(intptr,intptr)
From the error message I have determined that the error appears to occur durring the execution of line 25从错误消息中,我确定错误似乎发生在执行第 25 行期间
await App.Database.SavePartAsync(new Part
{
Name = partName.Text,
Description = partDescription.Text
});
in the NewPartsPage.xaml.cs file:在 NewPartsPage.xaml.cs 文件中:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using PartsManagement.Models;
namespace PartsManagement.Views
{
// Learn more about making custom code visible in the Xamarin.Forms previewer
// by visiting https://aka.ms/xamarinforms-previewer
[DesignTimeVisible(false)]
public partial class NewPartPage : ContentPage
{
public NewPartPage()
{
InitializeComponent();
}
async void Save_Clicked(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(partName.Text) && !string.IsNullOrWhiteSpace(partDescription.Text))
{
await App.Database.SavePartAsync(new Part
{
Name = partName.Text,
Description = partDescription.Text
});
}
await Navigation.PopModalAsync();
}
async void Cancel_Clicked(object sender, EventArgs e)
{
await Navigation.PopModalAsync();
}
}
}
Here is the Part class:这是 class 部分:
using SQLite;
namespace PartsManagement.Models
{
public class Part
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
}
and the database file:和数据库文件:
using System.Collections.Generic;
using System.Threading.Tasks;
using SQLite;
using PartsManagement.Models;
namespace PartsManagement
{
public class Database
{
readonly SQLiteAsyncConnection _database;
public Database(string dbPath)
{
_database = new SQLiteAsyncConnection(dbPath);
_database.CreateTableAsync<Part>().Wait();
_database.CreateTableAsync<Product>().Wait();
}
public Task<List<Part>> GetPartsAsync()
{
return _database.Table<Part>().ToListAsync();
}
public Task<int> SavePartAsync(Part aPart)
{
return _database.InsertAsync(aPart);
}
public Task<List<Product>> GetProductsAsync()
{
return _database.Table<Product>().ToListAsync();
}
public Task<int> SaveProductAsync(Part aProduct)
{
return _database.InsertAsync(aProduct);
}
}
}
The error seems to further indicate that the NewPartsPage cannot find the Part object, but I have referenced it with the proper using statement of using PartsManagement.Model;该错误似乎进一步表明 NewPartsPage 找不到零件 object,但我已通过使用 PartsManagement.Model 的正确使用语句引用它; .
. I am not sure if the square brackets in the error are significant.
我不确定错误中的方括号是否重要。 Is it attempting to find a specific instantiation of the Parts object as an array?
它是否试图将零件 object 的特定实例化为数组? I have reviewed the sqlite key words , but Part does not seem to be included in it.
我已经查看了sqlite 关键字,但其中似乎没有包含 Part。 Is my folder structure having an impact on the functionality I am looking for?
我的文件夹结构是否会影响我正在寻找的功能? I have performed this functionality in the local database tutorial , but this example is different in that I attempted to use a different folder structure.
我已在本地数据库教程中执行了此功能,但此示例的不同之处在于我尝试使用不同的文件夹结构。 Not sure how this would impact, but just in case I am bringing it up.
不知道这会如何影响,但以防万一我提出来。
This examples folder structure此示例文件夹结构
static Database database;
public static Database Database
{
get
{
if (database == null)
{
database = new Database(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Inventory.db3"));
}
return database;
}
}
Including the product class as it is key to the root cause of this issue.包括产品 class 因为它是导致此问题的根本原因的关键。
using SQLite;
namespace PartsManagement.Models
{
public class Product
{
[PrimaryKey, AutoIncrement]
public string ID { get; set; }
public string Name { get; set; }
public int Cost { get; set; }
public int OnHand { get; set; }
public int UsedIn { get; set; }
public double AveragePerProduct { get; set; }
public int NumberSold { get; set; }
public double Price { get; set; }
public Part[] PartsIn { get; set; }
}
}
it is failing when creating the Product
table because sqlite does not understand how to create an Part
array.创建
Product
表时失败,因为 sqlite 不了解如何创建Part
数组。 If you want to have tables with FK relations you need to add the SQLite Extensions package如果您想要具有 FK 关系的表,您需要添加SQLite Extensions package
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.