使用datamodel c#windowsStoreApp创建sqlitedb
本文关键字:sqlitedb 创建 c#windowsStoreApp datamodel 使用 | 更新日期: 2023-09-27 18:00:15
我正试图使用C#在Windows应用商店应用程序中本地创建一个sqlite数据库。
我一直收到错误
不了解sqliteDataGroup。。。
当使用下面的数据模型创建表时。我正在使用sqlite-net和扩展。
你知道我做错了什么吗?
public class sqliteDataItem
{
public sqliteDataItem()
{
}
public sqliteDataItem(int Id, String title, String subtitle, String Image, String description, String itemcontent, int groupid)
{
this.Id = Id;
this.Title = title;
this.Subtitle = subtitle;
this.Description = description;
this.Image = Image;
this.ItemContent = itemcontent;
this.GroupId = groupid;
}
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[ForeignKey(typeof(sqliteDataGroup))]
public int GroupId { get; set; }
public string Title { get; set; }
public string Subtitle { get; set; }
public string Description { get; set; }
public string Image { get; set; }
public string ItemContent { get; set; }
[ManyToOne]
public sqliteDataGroup sqliteDataGroup { get; set; }
public override string ToString()
{
return this.Title;
}
}
/// <summary>
/// Generic group data model.
/// </summary>
public class sqliteDataGroup
{
public sqliteDataGroup()
{
}
public sqliteDataGroup(int Id, String title, String subtitle, String Image, String description)
{
this.Id = Id;
this.Title = title;
this.Subtitle = subtitle;
this.Description = description;
this.Image = Image;
this.Items = new List<sqliteDataItem>();
}
[PrimaryKey,AutoIncrement]
public int Id { get; set; }
public string Title { get; set; }
public string Subtitle { get; set; }
public string Description { get; set; }
public string Image { get; set; }
[OneToMany]
public List<sqliteDataItem> Items { get; set; }
public override string ToString()
{
return this.Title;
}
}
首先,我建议添加facade,如果您想使用sqlite net ,它将非常有用
public static class SqLiteFacade
{
private static SQLiteConnection _sqlLiteConnection;
private static string _nameOfDatabase = "databaseName.db";
public static async Task<bool> InitializeDatabase()
{
try
{
_sqlLiteConnection = new SQLiteConnection(new SQLitePlatformWinRT(), await GetPathOfDatabaseInstance(_nameOfDatabase));
_sqlLiteConnection.CreateTable<SqliteDataItem>();
_sqlLiteConnection.CreateTable<SqliteDataGroup>();
return true;
}
catch (Exception ex)
{
return false;
}
}
public static SQLiteConnection DbConnection
{
get
{
return _sqlLiteConnection;;
}
}
public static async Task<string> GetPathOfDatabaseInstance(string databaseName)
{
if (!await DatabaseExist(databaseName))
{
var sampleFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(databaseName, CreationCollisionOption.ReplaceExisting);
}
var database = await ApplicationData.Current.LocalFolder.GetFileAsync(databaseName);
return database.Path;
}
private static async Task<bool> DatabaseExist(string databaseName)
{
var files = await ApplicationData.Current.LocalFolder.GetFilesAsync();
return files.Any(file => file.Name == databaseName);
}
}
您的型号:
public class SqliteDataGroup
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Title { get; set; }
public string Subtitle { get; set; }
public string Description { get; set; }
public string Image { get; set; }
[OneToMany(CascadeOperations = CascadeOperation.All)]
public List<SqliteDataItem> Items { get; set; }
}
public class SqliteDataItem
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string ItemContent { get; set; }
[ForeignKey(typeof(SqliteDataGroup))]
public int SqliteDataGroupId { get; set; }
}
现在您可以使用并验证您的解决方案:
if (await SqLiteFacade.InitializeDatabase())
{
var group = new SqliteDataGroup()
{
Description = "desc",
Title = "my title",
Image = "img",
Subtitle = "subtitle",
Items = new List<SqliteDataItem>()
{
new SqliteDataItem()
{
ItemContent = "Item 1"
},
new SqliteDataItem()
{
ItemContent = "Item 2"
},
new SqliteDataItem()
{
ItemContent = "Item 3"
}
}
};
SqLiteFacade.DbConnection.InsertWithChildren(group);
var results = SqLiteFacade.DbConnection.GetAllWithChildren<SqliteDataGroup>();
}
检查中的结果是否为操作结果。在我的情况下,我只使用这个裸体。首先卸载其他扩展。然后您可以尝试使用基于此的异步操作