如何在Xamarin Forms中连接两个表?

时间:2018-01-29 20:29:37

标签: android database xamarin xamarin.forms sqlite-net-pcl

这可能是一个愚蠢的问题,但我在使用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 
             }

在此之后,插入数据库的agerange被保存为null,我不知道为什么或者我可能做错了什么。所以,当我尝试使用viewmodel中的以下代码检索它时:

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

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

2 个答案:

答案 0 :(得分: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插入/更新它们时取得了更好的成功。这在编程模式中被称为Anemic domain model其中:&#34;贫血域模型是使用软件域模型,其中域对象包含很少或没有业务逻辑(验证,计算,业务规则等)。 )&#34;

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

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);
         }
}

}

答案 1 :(得分: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; }   
    }