[英]creating sqlite db using datamodel c# windows Store App
I'm trying to create a sqlite db locally in a Windows Store app using C#. 我正在尝试使用C#在Windows应用商店应用本地创建sqlite数据库。
I keep getting the error 我不断收到错误
don't know about sqliteDataGroup... 不知道sqliteDataGroup ...
when using the data model below to create the tables. 使用下面的数据模型创建表时。 I'm using sqlite-net and extensions. 我正在使用sqlite-net和扩展。
Any ideas what I'm, doing wrong? 任何想法我在做什么,错了吗?
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;
}
}
First of all I suggest add the facade which will be helpfull if you want to use sqlite net 首先,我建议添加外墙,如果您要使用sqlite网,这将很有帮助
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);
}
}
Your models: 您的模型:
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; }
}
Now you can use and valid your solution: 现在,您可以使用并验证您的解决方案:
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>();
}
Check if in the results are the results of operation. 检查结果是否为运算结果。 In my case I use only this nuget . 就我而言,我仅使用此nuget 。 The first of all uninstall other extensions. 首先卸载其他扩展。 Then you can try using async operations based on this 然后您可以尝试基于此使用异步操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.