2

这可能是一个愚蠢的问题,但我无法保存在 sqlite 中。我已成功使用 XF 的 facebook 登录,但也想将那里的信息保存在 sqlite-net-pcl 中。两种型号是:

public class FacebookProfile
    {
        public string Name { get; set; }
        public string Locale { get; set; }
        public string Link { get; set; }
        [OneToOne]
        [JsonProperty("age_range")]
        public AgeRange AgeRange { get; set; }
        [JsonProperty("first_name")]
        public string FirstName { get; set; }
        [JsonProperty("last_name")]
        public string LastName { get; set; }
        public string Gender { get; set; }
        public bool IsVerified { get; set; }
        [PrimaryKey]
        public int  Id { get; set; }
    }

public class AgeRange
    {
        public int Min { get; set; }
    } 

我已成功将信息下载到我的 FacebookProfile 模型并可以显示它。但是,当我尝试将它保存在我的 sqlite 数据库中时,FacebookProfile 的 AgeRange 属性在数据库中保存为 null 并且它应该具有一定的值。使用 sqlite 连接保存的代码是:

using (SQLite.SQLiteConnection conn = new SQLite.SQLiteConnection(App.DB_PATH))
            {
                conn.CreateTable<FacebookProfile>();
                conn.Insert(fp);   //fp is the FacebookProfile model info gotten from Facebook API and until this point, fp has agerange min value 
             }

此后,插入数据库的年龄范围保存为空,我不知道为什么或者我可能做错了什么。因此,当我尝试在视图模型中使用以下代码检索它时:

using (SQLite.SQLiteConnection conn = new SQLite.SQLiteConnection(App.DB_PATH))
            {
                conn.CreateTable<FacebookProfile>();
                FacebookProfile = conn.Table<FacebookProfile>().FirstOrDefault();
            }

从数据库中检索到的 FacebookProfile 的年龄范围值为 null,但其他我可以获得正确的信息。可能是什么问题?请帮忙!

4

2 回答 2

1

Sqlite-Net-pcl 不会识别自定义 poco 对象,例如:

public AgeRange AgeRange { get; set; }

请记住,Sqlite-net-pcl 基本上只是一个包装器,可以省去直接与 Sqlite 库交互的麻烦。因此,不必做这样的事情:

SQLiteCommand insertSQL = new SQLiteCommand("INSERT INTO FacebookProfile (Locale, Link, AgeRange, FirstName, LastNameGender, IsVerified, Id) VALUES (?,?,?,?,?,?,?)", conn);

insertSQL.Parameters.Add(facebookProfile.Locale);
    insertSQL.Parameters.Add(facebookProfile.Link);
    insertSQL.Parameters.Add(facebookProfile.AgeRange); //Assuming this is an int and not a custom type
    insertSQL.Parameters.Add(facebookProfile.FirstName);
    insertSQL.Parameters.Add(facebookProfile.LastNameGender);
    insertSQL.Parameters.Add(facebookProfile.IsVerified);
    insertSQL.Parameters.Add(facebookProfile.Id);

insertSQL.ExecuteNonQuery();

你可以简单地说:

using (SQLite.SQLiteConnection conn = new SQLite.SQLiteConnection(App.DB_PATH))
            {
                conn.CreateTable<FacebookProfile>();
                conn.Insert(fp);   //fp is the FacebookProfile model info gotten from Facebook API and until this point, fp has agerange min value 
             }

但是,它不知道如何处理您的AgeRange课程。那是一个字符串吗?那是一个int吗?就 Sqlite 而言,它是什么?正如其他人在评论中所说,包装器不知道如何将该类解释为数据库类型,例如 STRING、NUMERIC 等。

一般来说,我取得了更好的成功(使用 sqlite-net-pcl 时)创建表结构的精确副本作为中间类对象,并通过 FacebookProfile 的父类插入/更新它们。这在编程模式中被称为贫血域模型,其中:“贫血域模型是使用软件域模型,其中域对象包含很少或不包含业务逻辑(验证、计算、业务规则等)。”

在您的情况下,它可能看起来像这样:

public class dbFacebookProfile
{
    dbFacebookProfile(){}

    dbFacebookProfile(FacebookProfile fbProfile)
    {
        this.Name = fbProfile.Name;
        this.Locale = fbProfile.Locale;
        this.AgeRange fbProfile.AgeRange.Min; //or, however you want to modify this object to get the age range (Min?) from the object.
    }

public SaveToDB()
{
using (SQLite.SQLiteConnection conn = new SQLite.SQLiteConnection(App.DB_PATH))
        {
            conn.CreateTable<this>();
            conn.Insert(this);
         }
}

}

于 2018-01-30T00:12:45.790 回答
0

我认为既然 FacebookProfile 和 AgeRange 都可以说是逻辑相关的,你可以将这两者混合到一个表中:

public class FacebookProfile
    {
        public string Name { get; set; }
        public string Locale { get; set; }
        public string Link { get; set; }
        [OneToOne]
        [Ignore]
        [JsonProperty("age_range")]
        public AgeRange AgeRange
        {
           get => _ageRange;
           set
           {
             _ageRange = value;
             AgeMin = value?.Min ?? 0;
           }
        }
        [JsonProperty("first_name")]
        public string FirstName { get; set; }
        [JsonProperty("last_name")]
        public string LastName { get; set; }
        public string Gender { get; set; }
        public bool IsVerified { get; set; }
        [PrimaryKey]
        public int  Id { get; set; }

        [JsonIgnore]
        public int  AgeMin{ get; set; }   
    }
于 2018-01-30T06:48:45.350 回答