简体   繁体   English

使用DataModel C#Windows Store App创建sqlite db

[英]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.

相关问题 AC#Windows Store应用程序中带有SQLite的MBTiles数据库 - MBTiles db with SQLite in a c# windows store app 使用C#管理Windows Store应用程序的SQLite数据库 - Managing SQLite Database for Windows Store Apps using C# 使用C#在Windows应用商店应用中创建自定义图像 - Creating custom image in Windows store app with c# 使用C#在Windows Store App中裁剪并保存图像(UIElement) - Crop and save image (UIElement) in Windows Store App using c# with 在Windows应用商店应用WebView中使用请求发布数据 - 使用C# - Post data with a request in a Windows Store app WebView - using C# Windows Store App中使用XmlSerializer反序列化时出现C#错误 - C# Error while deserialize using XmlSerializer in Windows Store App 在Windows Store App中使用C#4.0库 - Using C# 4.0 library in Windows Store App Windows应用商店中使用HttpClient(C#)的摘要式身份验证 - Digest authentication in Windows Store app using HttpClient (C#) 尝试在Visual Studio 2015,c#,通用Windows应用程序中删除SQLite数据库中的条目,没有错误,但没有删除 - Trying to delete entries from SQLite db in Visual Studio 2015, c#, Universal Windows App, no errors, but not deletes 在Windows商店App中使用C#动态添加TextBlock - Dynamically adding TextBlock using C# in Windows store App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM