SQLite不了解类

时间:2015-09-18 15:03:37

标签: c# sqlite

我目前正在开发一个Windows窗体应用程序,我想使用一个不需要支持服务器或凭据来运行的简单数据库,所以我选择了SQLite。但到目前为止,试图让这件事情发挥作用是一场噩梦。

现在,我有一些简单属性的简单类,我想将它存储在数据库中。我已经为所有内容添加了相应的标签([Table("")]用于类,[PrimaryKey, AutoIncrement]用于Id属性),但每当我执行Connection.CreateTable(CreateFlags.AutoIncPK)时(它不会显示泛型参数,但它是在那里,我保证),它抛出一个NotSupportedException,说“不知道MyProject.MyClass”。我还在每个类中提供了一个空的无参数构造函数。

那么,我如何让SQLite“了解”我的班级?

编辑: 我的Character.cs文件:

[Table("Character")]
class Character
{
    [PrimaryKey, AutoIncrement]
    public int id { get; set; }

    public string Name
    {
        get;
        set;
    }
    public string FilePath
    {
        get;
        set;
    }
    public Character()
    {
    }
    public Character(string file)
    {
        this.FilePath = file;
        this.Name = FetchName(file);
    }

    private string FetchName(string file)
    {
        string[] fileHolder = File.ReadAllLines("\\chars\\" + file);
        foreach (string line in fileHolder)
        {
            if (line.ToLower().StartsWith("name"))
            {
                if (!line.Contains(';'))
                    return line.Split('=')[1].Trim().Trim('"');
                else return line.Split('=')[1].Split(';')[0].Trim().Trim('"');
            }
        }
        return string.Empty;
    }
}

我的Database.cs文件:

class Database
{
    private static SQLiteConnection Connection;
    private static string ConnectionString = "MyProject.sqlite";
    public Database()
    {
        if (!File.Exists("MyProject.sqlite"))
            System.Data.SQLite.SQLiteConnection.CreateFile("MyProject.sqlite");
        using (Connection = new SQLiteConnection(ConnectionString))
        {              
            Connection.CreateTable<Character>(CreateFlags.AutoIncPK);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

描述您的表的类必须是公开的。 在您的示例中,字符类不公开。

您应该像这样更改声明:

[Table("Character")]
public class Character
{
// your class code...
}

答案 1 :(得分:0)

我知道回答原始问题有点晚了,但是自从我的Google-Fu带领我来到这里以来,我希望这将对遇到相同错误消息的其他人有所帮助:

在我作为公共静态属性添加到具有[table]属性的类后,我开始收到此错误消息。将[忽略]属性添加到属性后,我不再收到错误消息。

相关问题