【问题标题】:cannot update model: it has no pk in windows phone 8.1 c#无法更新模型:在 windows phone 8.1 c# 中没有 pk
【发布时间】:2015-12-09 09:46:39
【问题描述】:

我有一个模型类,如下所示。

public class Section
{
    [PrimaryKey]
    public int section_id { get; set; }
    public string name { get; set; }
    public string url { get; set; }
    public System.DateTime syncTime { get; set; }
}

当我尝试更新时,我收到错误消息无法更新部分:它没有 PK

以下是我的更新代码

async public Task<bool> UpdateSection_SyncTime(int section_Id)
{
    try
    {
        var sections = await db.Table<Section>().ToListAsync();

        foreach (var section in sections)
        {
            if(section.section_id == section_Id)
            {
                section.syncTime = DateTime.Now;    // Update the Section Sync Time

                if (await db.UpdateAsync(section) >= 1)
                    return true;
            }
        }
    }
    catch { }
    return false;
}

在 SQLite.cs 文件中抛出异常并出现错误。

【问题讨论】:

    标签: c# sqlite windows-phone-8.1


    【解决方案1】:

    我希望对代码稍作修改可以解决问题

    foreach (var section in sections)
            {
                if(section.section_id == section_Id)
                {
    
                    section.syncTime = DateTime.Now;    // Update the Section Sync Time
                    App.DBConnection.Update(section); //update the db
    
                    if (await db.UpdateAsync(section) >= 1)
                        return true;
                }
            }
    

    【讨论】:

    • 我应该使用非异步方法吗?我是否需要打开和关闭连接?我的意思是应该使用的首选方法。
    • 我尝试使用 SQLiteConnection sqlConn = new SQLiteConnection(App.DBPath); sqlConn.Update(section);但还是同样的问题。
    • 它实际上解决了我正在使用 [SQLite.Net.Attributes.PrimaryKey] 我在某些站点中看到使用 [SQLite.PrimaryKey] 所以在使用这个命名空间后它不会给出错误。但是这两个命名空间有什么区别。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-06
    • 2016-03-16
    • 2016-11-21
    • 2015-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多