Xamarin.Forms SQLite 异常 - 不知道 PartsManagement.Models.Part[](<- 自定义 class)
Xamarin.Forms SQLite exception - don't know about PartsManagement.Models.Part[] (<- a custom class)
对于这个重新post 的问题,我感到抱歉,但是 original 从来没有以我可以理解和利用的方式回答。我不确定我是应该尝试重新提出这个问题还是产生一个新问题。
我试图在我创建的数据库中向我的零件 table 添加一个零件对象。我在执行期间收到以下错误
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:\Usersw3rv0\source\repos\PartsManagement\PartsManagement\PartsManagement\Database.cs:18
at PartsManagement.App.get_Database () [0x0000e] in C:\Usersw3rv0\source\repos\PartsManagement\PartsManagement\PartsManagement\App.xaml.cs:20
at PartsManagement.Views.NewPartPage.Save_Clicked (System.Object sender, System.EventArgs e) [0x00057] in C:\Usersw3rv0\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)
根据错误消息,我确定错误出现在第 25 行的执行过程中
await App.Database.SavePartAsync(new Part
{
Name = partName.Text,
Description = partDescription.Text
});
在 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();
}
}
}
这是第 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; }
}
}
和数据库文件:
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);
}
}
}
该错误似乎进一步表明 NewPartsPage 找不到 Part 对象,但我已使用 using PartsManagement.Model; 的正确使用语句引用了它。我不确定错误中的方括号是否重要。它是否试图将 Parts 对象的特定实例化为数组?我已查看 sqlite key words, but Part does not seem to be included in it. Is my folder structure having an impact on the functionality I am looking for? I have performed this functionality in the local database tutorial,但此示例的不同之处在于我尝试使用不同的文件夹结构。不确定这会产生怎样的影响,但以防万一我提出来。
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;
}
}
包括产品 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; }
}
}
创建 Product
table 时失败,因为 sqlite 不理解如何创建 Part
数组。如果你想要 tables 与 FK 关系,你需要添加 SQLite Extensions 包
对于这个重新post 的问题,我感到抱歉,但是 original 从来没有以我可以理解和利用的方式回答。我不确定我是应该尝试重新提出这个问题还是产生一个新问题。
我试图在我创建的数据库中向我的零件 table 添加一个零件对象。我在执行期间收到以下错误
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:\Usersw3rv0\source\repos\PartsManagement\PartsManagement\PartsManagement\Database.cs:18
at PartsManagement.App.get_Database () [0x0000e] in C:\Usersw3rv0\source\repos\PartsManagement\PartsManagement\PartsManagement\App.xaml.cs:20
at PartsManagement.Views.NewPartPage.Save_Clicked (System.Object sender, System.EventArgs e) [0x00057] in C:\Usersw3rv0\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)
根据错误消息,我确定错误出现在第 25 行的执行过程中
await App.Database.SavePartAsync(new Part
{
Name = partName.Text,
Description = partDescription.Text
});
在 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();
}
}
}
这是第 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; }
}
}
和数据库文件:
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);
}
}
}
该错误似乎进一步表明 NewPartsPage 找不到 Part 对象,但我已使用 using PartsManagement.Model; 的正确使用语句引用了它。我不确定错误中的方括号是否重要。它是否试图将 Parts 对象的特定实例化为数组?我已查看 sqlite key words, but Part does not seem to be included in it. Is my folder structure having an impact on the functionality I am looking for? I have performed this functionality in the local database tutorial,但此示例的不同之处在于我尝试使用不同的文件夹结构。不确定这会产生怎样的影响,但以防万一我提出来。
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;
}
}
包括产品 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; }
}
}
创建 Product
table 时失败,因为 sqlite 不理解如何创建 Part
数组。如果你想要 tables 与 FK 关系,你需要添加 SQLite Extensions 包